I'm confused with array and ownership(personal question )

trait A {
    fn take_ownership(self);
}

impl A for [char; 2] {
    fn take_ownership(self) {
        println!("content: {:?}", self);
    }
}

fn main() {
    println!("the type of [char; 2]: {}", std::any::type_name::<[char;2]>());
    let a = ['a', '1'];
    println!("a: {:#?}", a);
    a.take_ownership();  // consumer the ownership of the array `a`
    a.take_ownership(); // why this line can run success? 
}
playground addr: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a7aa3c00e5bcdfe744209c8f998010ff

[char; 2] is Copy (as is any [T; N] where T: Copy). With [String; 2] you get the expected behavior of "consuming ownership" - playground.

4 Likes

oh yes..... I understood instantly!
very appreciated for your reply, thanks a lot