-
In the bin folder, create a file called boxing.rs.
-
Add the following code and run it with cargo run --bin boxing:
1 use std::fs::File;
2 use std::io::BufReader;
3 use std::result::Result;
4 use std::error::Error;
5 use std::io::Read;
6 use std::fmt::Debug;
7
8 #[derive(Debug)]
9 struct Node<T> {
10 data: T,
11 child_nodes: Option<(BoxedNode<T>, BoxedNode<T>)>,
12 }
13 type BoxedNode<T> = Box<Node<T>>;
14
15 impl<T> Node<T> {
16 fn new(data: T) -> Self {
17 Node {
18 data,
19 child_nodes: None,
20 }
21 }
22
23 fn is_leaf(&self) -> bool {
24 self.child_nodes.is_none()
25 }
26
27 fn add_child_nodes(&mut self, a: Node<T>, b: Node<T>) {
28 assert!(
29 self.is_leaf(),
30 "Tried to add child_nodes to a node that is not a leaf"
31 );
32 self.child_nodes = Some((Box::new(a), Box...