-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstring.rs
58 lines (50 loc) · 1.46 KB
/
string.rs
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
#![allow(unused)]
// String = vector of u8 (Vec<u8>) valid UTF-8
// &str = slice of u8 (&[u8]) valid UTF-8
// When to use String vs &str
// String -> mutate or data needs to be owned
// &str -> read only
fn main() {
// String
let msg: String = String::from("Hello Rust 🦀");
let len: usize = msg.len();
println!("String length = {len}");
// str
// - String slice
// &str
// - usually use str with reference (borrowed)
// - immutable
let msg: String = String::from("Hello Rust 🦀");
// String slice
let s: &str = &msg[..5];
println!("slice = {s}");
let len: usize = s.len();
println!("slice length = {len}");
// String literal
// - stored inside binary
// - slice pointing to a specific part of the binary
// - immutable because hard-coded inside binary
let hello: &str = "Hello Rust";
// Multi line string literal
let s: &str = r#"
{ "a": 1,
"b": { "c": 2 },
"d": 3
}
"#;
println!("{s}");
// Deref coercion
// Rust automatically dereferences &String into a &str
let msg: String = String::from("Hello Rust 🦀");
let s: &str = &msg;
println!("slice = {s}");
// Add str to string
let mut msg = "Hello".to_string();
msg += " Rust";
println!("{msg}");
// String interpolation - format!
let lang = "Rust";
let emoji = "🦀";
let msg: String = format!("Hello {} {}", lang, emoji);
println!("{msg}");
}