Skip to content

Ownership error masks missing implementation of trait #27009

Closed
@Leopard2A5

Description

@Leopard2A5

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 borrowtestsas 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions