Rust的核心语言层面,只有一个字符串类型:字符串切片str 或&str。
字符串切片 : 对存储在其它地方、UTF-8编码的字符串的引用。
Rust 的标准库还包含了很多其它的字符串类型,例如:OsString、OsStr、CString。
String vs Str后缀 : 拥有或借用的变体。
fn main() {
    let s: String = "hello".to_string();
    println!("{}",s);
}fn main() {
    let s: String = String::from("hello world");
    println!("{}",s);
}fn main() {
    let mut s: String = String::from("hello world");
    s.push_str(" ...!");
    println!("{}",s);
}push_str() 不会获得变量的所有权。
注意单引号!!
fn main() {
    let mut s: String = String::from("hello world");
    s.push_str(" ...");
    s.push('!');
    println!("{}",s);
}+ 是把第一个字符串和第2个字符串的引用进行相加。
fn main() {
    let s: String = String::from("hello world");
    let s2 = s + &"..!".to_string();
    println!("{}", s2);
    // s1 变量在拼接后已经销毁
    //错误用法 : println!("{}", s);
}fn main() {
    let s: String = String::from("hello world");
    let s2 = format!("{}-{}", s, "!");
    println!("{}", s2);
    println!("{}", s);
}Rust 不允许对 String 进行索引的最后一个原因:
索引操作应消耗一个常量时间(O(1));
而String无法保证:需要遍历所有内容,来确定有多少个合法的字符;
可以使用[]和一个范围来创建字符串的切片(例子)
谨慎使用,如果切割时跨越了字符边界,程序就会panic。
fn main() {
    let s: String = String::from("hello world");
    let s2: String = s[0..2].to_string();
    println!("s2: {}", s2);
    println!("s: {}", s);
}fn main() {
    let s: String = String::from("hello 你好");
    let len: usize = s.len();
    println!("The length of '{}' is {}.", s, len);
}