Collections
Strings
let mut s = String::new(); // initializes an empty String
let data = "initial contents"; // is a string slice
let s = data.to_string(); // assign it to s, but first make it a String
let hi = "hi".to_string(); // or you could do this
let bye = String::from("bye"); // or this!fn main() {
let mut foo = String::from("foo");
let bar = "bar";
foo.push_str(&bar);
println!("foo is {}", foo);
println!("bar is {}", bar);
let mut lol = String::from("lo");
lol.push('l');
println!("lol is {}", lol);
let hello = String::from("Hello, ");
let world = String::from("world!");
let hello_world = hello + &world; // Note that hello has been moved here and can no longer be used
println!("hello_world is {}", hello_world);
let tic = String::from("tic");
let tac = String::from("tac");
let toe = String::from("toe");
// this code is unweildy and it also moves `tic`.
// if you want to see what the error looks like,
// uncomment out line 23
// let game = tic + "-" + &tac + "-" + &toe;
let game = format!("{}-{}-{}", tic, tac, toe);
println!("game is {}", game)
}Hash Maps
Last updated