0

I wanted to make copy of object i created so writed this code

struct values{
    value:i32,
}

impl values{
    fn returner(self)->values{
       return self 
    }
}

fn main(){
    let mut  data = values{
        value:10,
    };
    let z = data.returner();
    println!("{}",data.value);
    println!("{}",z.value);
    //values should be 10 10 for both as z is copy of data
}

but it says moves occur as doesn't implemente copy trait after implementing copy and clone trait it work perfectly i want to know how clone and copy are implmented internally

enter image description here

struct values{
    value:i32,
}
//implemented clone and copy by looking at documentation 
impl Copy for values{
    
}
impl Clone for values{
    fn clone(&self)->values{
        *self
    }
}
//wanted to return copy of values instance
impl values{
    fn returner(&self)->values{
       return *self 
    }
}

fn main(){
    let mut  data = values{
        value:10,
    };
    let a = data.clone();
    println!("{}",a.value);
    println!("{}",data.value);
}
//output as expected -> 10 
//10

0 Answers0