In Rust, why can calling `.clone()` on a `&T` or a `&str` hand back a reference instead of an owned value, and when is `to_owned` the right call?
answer
- every shared reference is Clone
- method lookup tries T, then &T
- Clone requires Sized
- borrowed form to owned form
basics
~20 sShared references are always Clone, so if T is not Clone, method lookup clones the &T and returns a reference. str cannot be Clone, so clone on a &str gives a &str; to_owned gives the owned form, a String or a Vec.
solid answer
~40 sWith a receiver `r: &T`, method lookup first tries `T`'s own `clone`, which needs `T: Clone`. If that fails it auto-references and finds `impl Clone for &T`, which always exists, so `r.clone()` returns another `&T`. For a concrete non-`Clone` type this compiles, and rustc's default `noop_method_call` lint warns "call to `.clone()` on a reference in this situation does nothing". In generic code, `fn duplicate<T>(item: &T) -> T { item.clone() }` fails with E0308, "expected type parameter `T`, found `&T`", and the fix is a `T: Clone` bound. A `&str` hits the same path, because `str` is unsized and `Clone` requires `Sized`. `ToOwned` exists for that case: `str::to_owned` returns a `String`, `[T]::to_owned` a `Vec<T>`. Use `to_owned` to turn a borrowed view into owned data, and `clone` to duplicate a value you already own.
code
rust · 5 linesfn duplicate<T>(item: &T) -> T {
item.clone()
}
fn main() {}go deeper
Recall that clone on a &str gives back a &str, and that to_owned or to_string is how you get an owned String.
Explain method lookup: T's own clone is tried first, and only when T is not Clone does the always-present clone for &T apply.
Diagnose the E0308 found &T error in generic code at sight, add the T: Clone bound, and treat a noop_method_call warning as a real bug signal.
Set the expectation that clones in API code are deliberate: reviewers check what each clone returns and whether a borrowed parameter would serve.
## Method lookup picks the first clone that applies `x.clone()` is not a fixed function call; the compiler resolves it by **method lookup**. When the receiver has type `&T`, it tries candidates in order: 1. A `clone` whose `&self` receiver is `&T`, that is `<T as Clone>::clone`. This needs `T: Clone`. 2. If that fails, it **auto-references** the receiver to `&&T` and finds `<&T as Clone>::clone`. That impl always exists: the standard library implements `Clone` for `&T` with no `Clone` bound on `T`, under the comment "Shared references can be cloned, but mutable references *cannot*!" So when `T` is `Clone` you get an owned `T`, and when it is not you silently get **another reference**. The `Clone::clone` documentation says the same: for reference types like `&T`, cloning "creates another reference to the same value". ## A concrete type: it compiles, with a warning ```rust struct Atlas { pages: u32 } // not Clone let r = &atlas; let r2 = r.clone(); // r2: &Atlas, not Atlas ``` This compiles, but rustc's default `noop_method_call` lint warns: ```plaintext warning: call to `.clone()` on a reference in this situation does nothing = note: the type `Atlas` does not implement `Clone`, so calling `clone` on `&Atlas` copies the reference, which does not do anything and can be removed ``` The usual cause is a missing `#[derive(Clone)]`; the lint's help suggests adding it. ## Generic code: E0308 In a generic function the mistake turns into a type error: ```rust fn duplicate<T>(item: &T) -> T { item.clone() // error[E0308]: mismatched types } ``` rustc reports "expected type parameter `T`, found `&T`" with the note "`T` does not implement `Clone`, so `&T` was cloned instead", and suggests `fn duplicate<T: Clone>`. Replacing `clone` with `to_owned` does not help (the blanket `ToOwned` impl also requires `T: Clone`), and `*item` fails with E0507 because it would move out of a shared reference. A close variant behaves differently: `items[0].clone()` on `items: &[T]` with no bound fails with **E0599**, "no method named `clone` found for type parameter `T`". There the receiver is a place of type `T`, not a reference, so lookup tries the receiver types `T` and `&T`, both of which mean `Self = T` for `clone(&self)`, and never reaches `&&T`. ## str and slices: why Clone cannot help `Clone` has `Sized` as a supertrait, and `str` and `[T]` are **unsized**, so they can never implement it. A `&str` therefore always takes the reference path: `"player".clone()` is a `&str` (and triggers the same `noop_method_call` warning). **`ToOwned`** (in `std::borrow`) is the trait for going from a borrowed form to its owned counterpart: | Receiver | `.clone()` returns | `.to_owned()` returns | |---|---|---| | `&str` | `&str` | `String` | | `&[T]` (with `T: Clone`) | `&[T]` | `Vec<T>` | | `&T` with `T: Clone` | `T` | `T` (the blanket impl calls `clone`) | | `&T` with `T` not `Clone` | `&T` | `&T` | `"player".to_string()` also yields a `String`; it comes from `ToString`, which the standard library implements for every `Display` type. Both are idiomatic for `str`; `to_owned` states the borrowed-to-owned intent and works for slices too. ## What a strong answer covers 1. Explain the lookup order: `T`'s own `clone` first, then the `clone` for `&T` through auto-referencing. 2. Name the two symptoms: a `noop_method_call` warning for concrete types, an E0308 "found `&T`" error in generic code. 3. Explain why `str` and `[T]` can never be `Clone` (they are unsized) and why `ToOwned` exists for them. 4. Pick the right call: `to_owned` for borrowed-to-owned, `clone` for duplicating an owned value, a `T: Clone` bound in generic code. ## What to check in a review - A `.clone()` whose result is stored or returned where an owned type is expected: confirm `T: Clone` holds, or the code is cloning a reference. - A `noop_method_call` warning: almost always a missing derive or a misplaced `.clone()`, never something to silence blindly. - `.clone()` on a `&str` or `&[T]` that was meant to produce owned data: replace it with `to_owned()` (or `to_string()` / `to_vec()`). - A generic function returning `T` from `&T`: it needs `T: Clone`; adding `Copy` instead over-constrains callers. Cloning through a reference-counted pointer has its own rules and costs, covered with smart pointers.
- Why does `items[0].clone()` on `items: &[T]` with no bound give E0599 rather than E0308?The receiver `items[0]` is a place of type `T`, not a reference. Lookup tries `T` and then `&T` as the receiver of `Clone::clone(&self)`, both meaning `Self = T`, and never reaches `&&T`. With no `T: Clone` nothing matches, so rustc reports E0599, "no method named `clone` found for type parameter `T`".
- Is `"abc".to_string()` different from `"abc".to_owned()`?Both produce an equal `String`. `to_string` comes from `ToString`, implemented for every `Display` type; `to_owned` comes from `ToOwned`, the borrowed-to-owned trait. For `str` either is idiomatic. `to_owned` also covers slices, where `to_string` does not apply.
- clone() on a reference always clones the value it points to
- &str implements Clone, so label.clone() returns a String
- to_owned and clone always return the same type
- Swapping clone() for to_owned() removes the need for a T: Clone bound
- ToOwned is implemented only for str, so it cannot help with slices