Shared references
Quick answer
A shared reference allows code to read an existing value without taking its ownership. Write &value to borrow it and use a parameter such as &String to accept that borrow.
Borrow rather than consume
A reference must point to a valid value for the duration it is used. Several shared references can coexist. For ordinary data, they do not allow mutation through the reference; interior mutability is a later exception with its own rules. A reference parameter does not become an owner of the referenced value.
fn length(text: &String) -> usize { text.len() }fn main() { let name = String::from("Ada"); println!("{}: {}", name, length(&name));}Expected stdout (not executed):
Ada: 3The function borrows name and the caller can still use it. A more flexible text parameter &str appears later.
fn main() { let value = 42; let first = &value; let second = &value; println!("{} {}", *first, *second);}Expected stdout (not executed):
42 42The asterisk dereferences each shared reference to read the integer.
Common mistake
Passing a String where a function expects &String is not the same as borrowing it. Use the ampersand at the call site.
Try it yourself
Write a borrowed function that returns whether a String has zero length. Keep using the String after calling it.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Shared references — practice quiz
Choose one answer per question, then review the explanation.
Can two shared references read the same ordinary String concurrently within their valid borrows?
Does a function receiving `&String` own the string's heap buffer?
For ordinary data, which action is permitted through a shared reference?