Why is it discouraged to accept a reference &String, &Vec, or &Box as a function argument?
@lebyy▲ 0 net votes
Problem: I wrote some Rust code that takes a &String as an argument: fn awesome_greeting(name: &String) { println!("Wow, you are awesome, {}!", name); } I've also written code that takes in a reference to a Vec or Box : fn total_price(prices: &Vec<i32>) -> i32 {… Fix (from the top answer): TL;DR: One can instead use &str , &[T] or &T to allow for more generic code. One of the main reasons to use a String or a Vec is because they allow increasing or decreasing the capacity. However, when you accept an immutable reference, you cannot use any of those interesting methods on the Vec or String . Accepting a…