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 {
19411941~~~~
19421942
19431943However, 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:
19451947
19461948~~~~
19471949// This does
19481950fn head<T: Copy>(v: &[T]) -> T {
1949- v[0]
1951+ copy v[0]
19501952}
19511953~~~~
19521954
@@ -2137,7 +2139,7 @@ as in this version of `print_all` that copies elements.
21372139fn print_all<T: Printable + Copy>(printable_things: ~[T]) {
21382140 let mut i = 0;
21392141 while i < printable_things.len() {
2140- let copy_of_thing = printable_things[i];
2142+ let copy_of_thing = copy printable_things[i];
21412143 copy_of_thing.print();
21422144 i += 1;
21432145 }
You can’t perform that action at this time.
0 commit comments