34 lines
840 B
Rust
34 lines
840 B
Rust
fn main() {
|
|
let mut s = String::new();
|
|
|
|
let data = "initial contents";
|
|
let s = data.to_string();
|
|
|
|
// this also works
|
|
let s = "initial contents".to_string();
|
|
|
|
// Updating a String
|
|
let mut s = String::from("foo");
|
|
s.push_str("bar");
|
|
|
|
let mut s1 = String::from("foo");
|
|
let s2 = "bar";
|
|
s1.push_str(s2);
|
|
println!("s2 is {s2}");
|
|
|
|
// push method takes a character and not a string
|
|
let mut s = String::from("lo");
|
|
s.push('l');
|
|
|
|
let s1 = String::from("tic");
|
|
let s2 = String::from("tac");
|
|
let s3 = String::from("toe");
|
|
|
|
let s_total = format!("{s1}-{s2}-{s3}");
|
|
|
|
// Rust doesn't allow indexing of Strings.
|
|
// UTF-8 characters can take over 2 bytes which makes it
|
|
// not nice for indexing. For rush, just use a vec of chars
|
|
// straight for the string list.
|
|
}
|