Deepcopy: Difference between revisions

(Removing omission from C after adding implementation.)
Line 1,525:
This is what the <code>Clone</code> trait exists for although the depth of the copy is arbitrary and up to the type that implements the trait.
 
<lang rust> // The compiler can automatically implement Clone on structs (assuming all members have implemented Clone).
#[derive(Clone)]
struct Tree<T> {
Line 1,536:
 
impl<T> Tree<T> {
fn root(ddata: T) -> Self {
TreeSelf { left: None, data: d, right: None }
}
 
fn leaf(d: T) -> Leaf<T> {
Some(Box::new(TreeSelf::root(d)))
}
}
 
 
fn main() {
let mut tree = Tree::root(vec![4, 5, 6]);
tree.right = Tree::leaf(vec![1, 2, 3]);
tree.left = Tree::leaf(vec![7, 8, 9]);
 
let newtree = tree.clone();
}</lang>