gw-rust-programming-tutorial 0.1.0

gw rust test.
Documentation
pub fn test_string() {
    //新建字符串
    let mut s = String::new();

    //这新建了一个叫做 s 的空的字符串,接着我们可以向其中装载数据。通常字符串会有初始数据,因为我们希望一开始就有这个字符串。为此,可以使用 to_string 方法,它能用于任何实现了 Display trait 的类型,字符串字面量也实现了它。示例 8-12 展示了两个例子。
    let data = "initial contents";

    let s = data.to_string();

    // 该方法也可直接用于字符串字面量:
    let s = "initial contents".to_string();

    //string是utf8存储的
    let hello = String::from("السلام عليكم");
    let hello = String::from("Dobrý den");
    let hello = String::from("Hello");
    let hello = String::from("שָׁלוֹם");
    let hello = String::from("नमस्ते");
    let hello = String::from("こんにちは");
    let hello = String::from("안녕하세요");
    let hello = String::from("你好");
    let hello = String::from("Olá");
    let hello = String::from("Здравствуйте");
    let hello = String::from("Hola");

    update_string();
    test_slice();
}

//更新字符串
fn update_string() {
    //使用 push_str 和 push 附加字符串
    let mut s = String::from("foo");
    s.push_str("bar");

    //使用 push_str 方法向 String 附加字符串 slice
    let mut s1 = String::from("foo");
    let s2 = "bar";
    s1.push_str(s2);
    println!("s2 is {}", s2);

    //push 方法被定义为获取一个单独的字符作为参数,并附加到 String 中。示例 8-17 展示了使用 push 方法将字母 l 加入 String 的代码。
    let mut s = String::from("lo");
    s.push('l');
    println!("s is {}", s);

    //使用 + 运算符或 format! 宏拼接字符串
    let s1 = String::from("Hello, ");
    let s2 = String::from("world!");
    let s3 = s1 + &s2; // 注意 s1 被移动了,不能继续使用

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

    let s = format!("{}-{}-{}", s1, s2, s3);
    println!("s is {}", s);
}

//测试字符串切片
fn test_slice() {
    let hello = "Здравствуйте";

    let s = &hello[0..4];
    println!("slice s= {} len={}",s,hello.len());

    //遍历字符串的方法
    for c in hello.chars() {
        println!("{}", c);
    }

    for c in hello.bytes() {
        println!("{}", c);
    }
}