Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/Deepcopy/Rust/deepcopy.rust
Normal file
27
Task/Deepcopy/Rust/deepcopy.rust
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
// The compiler can automatically implement Clone on structs (assuming all members have implemented Clone).
|
||||
#[derive(Clone)]
|
||||
struct Tree<T> {
|
||||
left: Leaf<T>,
|
||||
data: T,
|
||||
right: Leaf<T>,
|
||||
}
|
||||
|
||||
type Leaf<T> = Option<Box<Tree<T>>>;
|
||||
|
||||
impl<T> Tree<T> {
|
||||
fn root(data: T) -> Self {
|
||||
Self { left: None, data, right: None }
|
||||
}
|
||||
|
||||
fn leaf(d: T) -> Leaf<T> {
|
||||
Some(Box::new(Self::root(d)))
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut tree = Tree::root([4, 5, 6]);
|
||||
tree.right = Tree::leaf([1, 2, 3]);
|
||||
tree.left = Tree::leaf([7, 8, 9]);
|
||||
|
||||
let newtree = tree.clone();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue