I am quite new to Rust, and I tried to implement generic binary trees. I used this code as a reference for enums and impl.
Here, use List::*;
seems to refer to the enum below.
Here is my code for my trees:
use BinaryTree::*;
enum BinaryTree<T> {
Empty,
Node(Box<(T, BinaryTree<T>, BinaryTree<T>)>),
}
impl<T: Ord> BinaryTree<T> {
fn add(&mut self, value: T) {
match *self {
BinaryTree::Empty => {
*self = BinaryTree::Node(Box::new((value, BinaryTree::Empty, BinaryTree::Empty)))
}
BinaryTree::Node(ref mut node) => {
if value <= node.0 {
node.1.add(value);
} else {
node.2.add(value);
}
}
}
}
fn height(&self) -> i32 {
match *self {
BinaryTree::Empty => -1i32,
BinaryTree::Node(ref node) => 1i32 + maxi(node.1.height(), node.2.height()),
}
}
fn size(&self) -> i32 {
match *self {
BinaryTree::Empty => 0i32,
BinaryTree::Node(ref node) => 1i32 + node.1.size() + node.2.size(),
}
}
}
fn maxi(x: i32, y: i32) -> i32 {
if x > y {
x
} else {
y
}
}
Then cargo exits with this error:
error: unresolved import `BinaryTree::*`. Maybe a missing `extern crate BinaryTree`? [E0432]
use BinaryTree::*;
^~~~~~~~~~
According to what I understood in the example of the linked lists, rustc should understand that use BinaryTree::*;
is the enum I define below. This could help changing all the BinaryTree::Empty
to Empty
and BinaryTree::Node(...)
to Node(...)
.
Edit:
Since I would like to developp a library and use multiple files, I work in src/trees.rs
in main.rs, I have only (for now)
mod trees;
fn main() {}