Avatar ·

Why is it not recommended to accept &String, &Vec, and &Box as function arguments in Rust?

Hello everyone! I recently started learning Rust and noticed a strange thing: in many tutorials and on Stack Overflow, they write that you shouldn't write functions that accept `&String`, `&Vec`, or `&Box`. Instead, they advise using `&str`, `&[T]`, and just `&T` (or `&T` for Box). But I don't quite understand what the problem is. Here's a simple example I wrote for working with text: ```rust fn print_length(s: &String) { println!("String length: {}", s.len()); } fn main() { let my_string = String::from("Hello, world!"); print_length(&my_string); } ``` This code works. Why is it considered bad style? Why is `&String` worse than `&str`? And the same with vectors: why is `&Vec` worse than `&[i32]`? After all, in both cases I'm passing a reference, not ownership. I'm confused that