Closed
Description
I would expect the main function to not compile because clone() has not been implemented for struct Test. However this error is masked by:
src/main.rs:26:2: 26:7 error: cannot borrow
testsas mutable because it is also borrowed as immutable
The error here is the missing implementation though, because it solves the ownership issue. rustc doesn't complain about the missing implementation at all.
#[derive(Debug)]
struct Test {
name: String
}
// impl std::clone::Clone for Test {
// fn clone(&self) -> Test {
// Test {
// name: self.name.clone()
// }
// }
// }
fn main() {
let mut tests = Vec::new();
tests.push(Test{ name: "test1".to_string() });
tests.push(Test{ name: "test2".to_string() });
tests.push(Test{ name: "test3".to_string() });
tests.sort_by(|a, b| a.name.cmp(&b.name));
println!("{:?}", tests);
let test = tests.last().unwrap().clone();
println!("{:?}", test);
tests.sort_by(|a, b| b.name.cmp(&a.name));
println!("{:?}", tests);
}
Metadata
Metadata
Assignees
Labels
No labels