-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path023rd_box.rs
More file actions
41 lines (35 loc) · 998 Bytes
/
023rd_box.rs
File metadata and controls
41 lines (35 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#![allow(unused)]
use std::fmt::Display;
use std::io;
use rand::Rng;
use std::io::{Write, BufReader, BufRead, ErrorKind};
use std::fs::File;
use std::cmp::Ordering;
use std::ops::Add; //for generic
use std::collections::HashMap; //for HashMap
fn main() {
// smart pointers, box
let b_int1 = Box::new(10);
println!("b_int1 = {}", b_int1);
struct TreeNode<T> {
pub left: Option<Box<TreeNode<T>>>,
pub right: Option<Box<TreeNode<T>>>,
pub key: T,
}
impl<T> TreeNode<T> {
pub fn new(key: T) -> Self {
TreeNode { left: None, right: None, key,
}
}
pub fn left(mut self, node: TreeNode<T>) -> Self {
self.left = Some(box::new(node));
self
}
pub fn right(mut self, node: TreeNode<T>) -> Self {
self.right = Some(box::new(node));
self
}
}
let node1 = TreeNode::new(1)
.left(TreeNode::new(2)).right(TreeNode::new(3));
}