What's the difference between say(&word) and say(word)

This script works, but what's the difference between ?

fn main() {
    let v = vec!["hello", "world"];
    fn say(word: &str) {
        println!("say: {}", word);
    }
    let word = v[0];
    say(word); // <----- this
    say(&word); // <----- and this
    println!("word is in: {}", word);
}

And what's the best practices for String and &str?
Thank you!

word is &str, so &word is &&str.
When calling say, there's an automatic coercion going on to turn &&str into &str

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.