Why is it discouraged to accept a reference to a String (&String), Vec (&Vec), or Box (&Box) as a function argument?

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 { prices.iter().sum() } fn is_even(value: &Box<i32>) -> bool { **value % 2 == … Read more

What is the equivalent of the join operator over a vector of Strings?

I wasn’t able to find the Rust equivalent for the “join” operator over a vector of Strings. I have a Vec<String> and I’d like to join them as a single String: let string_list = vec![“Foo”.to_string(),”Bar”.to_string()]; let joined = something::join(string_list,”-“); assert_eq!(“Foo-Bar”, joined); Related: What’s an idiomatic way to print an iterator separated by spaces in Rust? … Read more

Why is there a large performance impact when looping over an array with 240 or more elements?

When running a sum loop over an array in Rust, I noticed a huge performance drop when CAPACITY >= 240. CAPACITY = 239 is about 80 times faster. Is there special compilation optimization Rust is doing for “short” arrays? Compiled with rustc -C opt-level=3. use std::time::Instant; const CAPACITY: usize = 240; const IN_LOOPS: usize = … Read more