- &
Rust ownership borrow string in function
- When passing the variable we need to prefix it with &.
- In the function definition we also include the & in-front of the type.
- Inside the function we can prefix it with * to dereference the variable but in general we don't need to as Rust figures that out.
examples/ownership/string-function-borrow/src/main.rs
fn main() { let name = String::from("Foo Bar"); greet(&name); greet(&name); } fn greet(text: &String) { println!("Greet: {}", *text); // explicit derefernce println!("Greet: {}", text); // automatic dereference println!("Greet: {text}"); }