字符串slice是String的一部分引用
字面值就是slice
其他类型的slice
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| fn main()
{ let s = String::from("Hello World");
let h = &s[0..5]; let w = &s[6..11]; let w = &s[6..=10]; let w = &s[..]; println!("w= {}",w);
let ss = String::from("你好"); let w1 = &s[0..1]; println!("h = {}",h);
let s3 = "hh"; let a = [1,2,3,4]; let sss = &a[1..3]; println!("sss = {}",sss[0]); println!("sss = {}",sss[1]); }
|