`

rust字符串

    博客分类:
  • rust
 
阅读更多
字符串转数字,(::<>)的形式叫作turbofish操作符
    let str = "1";
    let i:i32 = str.parse().unwrap();
    let i2:i32 = str.parse::<i32>().unwrap();


fn main() {
    let data = "aaa".to_string();
    println!("{}",data);
    let data2 = "bbb";
    println!("{}",data2);
    let data3 = data+&data2;
    //字符串连接直接用+
    println!("data3:{}",data3);


    let s1 = String::from("tic");
    let s2 = String::from("tac");
    let s3 = String::from("toe");

    //也可以用format格式化
    let s4 = format!("{}-{}-{}", s1, s2, s3);
    println!("s4:{}",s4);

    let hello = "abcdefg";
    //遍历字符串
    for c in hello.chars(){
        print!("{} ",c)
    }
    println!();
    //切片
    let s = &hello[0..4];
    println!("s:{}",s)
}

aaa
bbb
data3:aaabbb
s4:tic-tac-toe
a b c d e f g 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics