mozilla/rust — pull request #9521 by Kimundi (2013-09-26 12:48:20+00:00)
Title: Added standalone clone() and deep_clone() functions.

Right now, if you have an `Iterator<&T>`, like you get with `vec.iter()`, then for copying the contents of it you have to do either of these:

~~~
// Only works for implicit copyable T, and can be confusing due to many pointers/sigils:
[1, 2, 3, 4].iter().map(|x| *x)...
[1, 2, 3, 4].iter().map(|&x| x)...

// Works for anything cloneable, but is long and can confuse the matters more due to autoderef:
[1, 2, 3, 4].iter().map(|x| x.clone())...
~~~

Having a standalone function of `clone` eases the situation: It's signature is `&T -> T`, and `map` transforms `A -> B`. Therefore `map(clone)` is easily explainable as removing an layer of `&` by making an copy through it:
~~~
[1, 2, 3, 4].iter().map(clone)...
~~~

Just like `std::from_str::from_str`, these two functions are basically just workarounds for not being able to import associated functions from traits.