gw_rust_programming_tutorial/chapter_8/
test_string.rs1pub fn test_string() {
2 let mut s = String::new();
4
5 let data = "initial contents";
7
8 let s = data.to_string();
9
10 let s = "initial contents".to_string();
12
13 let hello = String::from("السلام عليكم");
15 let hello = String::from("Dobrý den");
16 let hello = String::from("Hello");
17 let hello = String::from("שָׁלוֹם");
18 let hello = String::from("नमस्ते");
19 let hello = String::from("こんにちは");
20 let hello = String::from("안녕하세요");
21 let hello = String::from("你好");
22 let hello = String::from("Olá");
23 let hello = String::from("Здравствуйте");
24 let hello = String::from("Hola");
25
26 update_string();
27 test_slice();
28}
29
30fn update_string() {
32 let mut s = String::from("foo");
34 s.push_str("bar");
35
36 let mut s1 = String::from("foo");
38 let s2 = "bar";
39 s1.push_str(s2);
40 println!("s2 is {}", s2);
41
42 let mut s = String::from("lo");
44 s.push('l');
45 println!("s is {}", s);
46
47 let s1 = String::from("Hello, ");
49 let s2 = String::from("world!");
50 let s3 = s1 + &s2; let s1 = String::from("tic");
53 let s2 = String::from("tac");
54 let s3 = String::from("toe");
55
56 let s = format!("{}-{}-{}", s1, s2, s3);
57 println!("s is {}", s);
58}
59
60fn test_slice() {
62 let hello = "Здравствуйте";
63
64 let s = &hello[0..4];
65 println!("slice s= {} len={}",s,hello.len());
66
67 for c in hello.chars() {
69 println!("{}", c);
70 }
71
72 for c in hello.bytes() {
73 println!("{}", c);
74 }
75}