1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| {% note info %} 实际上rust核心中一般使用str类型,很少出现string,这种是在编译库中实现的 {% endnote%}
fn main() { let mut s0 = String::new(); s0.push_str("hello"); println!("s0 = {}",s0);
let s1 = String::from("init some thing"); println!("s1 = {}",s1); let s1 = "init som thing".to_string(); println!("s1 = {}",s1); let mut s2 = String::from("hello"); s2.push_str(",world"); println!("s2 = {}",s2);
let ss = "!".to_string(); s2.push_str(&ss); println!("s2 = {}",s2); println!("ss = {}",ss); let mut s2 = String::from("tea"); s2.push('m'); println!("s2 = {}",s2); let s1 = "hello".to_string; let s2 = String::from(",world"); let s3 = s1 + &s2; println!("s3 = {}",s3); println!("s2 = {}",s2); let s341 = String::from("tic"); let s342 = String::from("tac"); let s343 = String::from("toe"); let s344 = format!("{}-{}-{}",s341,s342,s343); println!("s344 = {}",s344); let s4 = String::from("hello"); let s41 = s4[0]; println!("s4.len = {}",s4.len()); let s4 = String::from("你好"); println!("s4.len = {}",s4); let hello = "你好"; let h5 = &hello[0..3]; println!("h5 = {}",h5); for c in s4.chars() { println!("c = {}",c); } for b in s4.bytes() { println!("b = {}",b); } }
|