[Rust] String vs String slice

https://doc.rust-lang.org/std/string/struct.String.html#method.trim

 

fn string_slice(arg: &str) {
    println!("{}", arg);
}
fn string(arg: String) {
    println!("{}", arg);
}

fn main() {
    string_slice("blue");
    string("red".to_string());
    string(String::from("hi"));
    string("rust is fun!".to_owned());
    string_slice("nice weather".into()); // this can be both string or string_slice, deps on the origin value
    string(format!("Interpolation {}", "Station"));
    string_slice(&String::from("abc")[0..1]);
    string_slice("  hello there ".trim());
    string("Happy Monday!".to_string().replace("Mon", "Tues"));
    string("mY sHiFt KeY iS sTiCkY".to_lowercase());
}

 

posted @ 2024-02-26 03:15  Zhentiw  阅读(13)  评论(0)    收藏  举报