File tree Expand file tree Collapse file tree 1 file changed +5
-3
lines changed Expand file tree Collapse file tree 1 file changed +5
-3
lines changed Original file line number Diff line number Diff line change @@ -1941,12 +1941,14 @@ fn head_bad<T>(v: &[T]) -> T {
1941
1941
~~~~
1942
1942
1943
1943
However, we can tell the compiler that the ` head ` function is only for
1944
- copyable types: that is, those that have the ` Copy ` trait.
1944
+ copyable types: that is, those that have the ` Copy ` trait. In that
1945
+ case, we can explicitly create a second copy of the value we are
1946
+ returning using the ` copy ` keyword:
1945
1947
1946
1948
~~~~
1947
1949
// This does
1948
1950
fn head<T: Copy>(v: &[T]) -> T {
1949
- v[0]
1951
+ copy v[0]
1950
1952
}
1951
1953
~~~~
1952
1954
@@ -2137,7 +2139,7 @@ as in this version of `print_all` that copies elements.
2137
2139
fn print_all<T: Printable + Copy>(printable_things: ~[T]) {
2138
2140
let mut i = 0;
2139
2141
while i < printable_things.len() {
2140
- let copy_of_thing = printable_things[i];
2142
+ let copy_of_thing = copy printable_things[i];
2141
2143
copy_of_thing.print();
2142
2144
i += 1;
2143
2145
}
You can’t perform that action at this time.
0 commit comments