0

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() {}
Guinness
  • 39
  • 6
  • 1
    The code as presented [compiles successfully](https://play.rust-lang.org/?gist=2231a51461b58d0c6cea11aa9166db08&version=stable&backtrace=0), and changing all the `BinaryTree::Empty` to `Empty` also works. Can you clarify what the problem is? – Shepmaster Jun 27 '16 at 17:37
  • `maxi` is a built in function in [the standard library](http://doc.rust-lang.org/std/cmp/fn.max.html) – Shepmaster Jun 27 '16 at 17:40
  • Perhaps your code isn't at the root but is in a module? If so this may be a duplicate of [this](http://stackoverflow.com/q/33948293/155423), or [this](http://stackoverflow.com/q/31035491/155423) or [this](http://stackoverflow.com/q/26224947/155423)? – Shepmaster Jun 27 '16 at 17:47

1 Answers1

0

Thanks to the comment of Shepmaster, I found out the issue.

The path must be absolute when using use

Changing use BinaryTree::*; to use trees::BinaryTree::*; solves the issue.

This problem is solved.

Guinness
  • 39
  • 6