Keeping your object safe

We can't use all traits to create a trait object. Take the following:

let my_vec = vec![1,3,5,7,9]; 
let dupe = &my_vec as &Clone;

This will not compile as Clone is not object-safe as Clone contains Self: Sized, which a trait cannot have.

If the trait doesn't require Self: Sized and all of the methods are object-safe, it is an object-safe trait. For a method to be object-safe, it must require Self: Sized. If the method doesn't require Self: Sized, it can still be object-safe if the method doesn't require any parameters and doesn't use Self.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset