Rust Strings
Rust has two string types: &str, a view of text, and String, text you own and can grow.
ExampleRust
fn main() {
let mut s = String::from("Hello");
s.push_str(", Rust");
s.push('!');
println!("{} ({} bytes)", s, s.len());
println!("{}", s.to_uppercase());
println!("{}", s.replace("Rust", "World"));
println!("{}", s.contains("Rust"));
let parts: Vec<&str> = "a,b,c".split(',').collect();
println!("{:?}", parts);
println!("{}", &s[0..5]);
println!("{}", format!("{}-{}", "x", 1));
}Output
Hello, Rust! (12 bytes) HELLO, RUST! Hello, World! true ["a", "b", "c"] Hello x-1
Quick check
Which string type can grow?
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.