Cornstalks
Cornstalks

Reputation: 38228

Trying to `use` enum results in "unresolved import"

Following the Rust example on enum:

// Allow Cons and Nil to be referred to without namespacing
use List::{Cons, Nil};

// A linked list node, which can take on any of these two variants
enum List {
    // Cons: Tuple struct that wraps an element and a pointer to the next node
    Cons(uint, Box<List>),
    // Nil: A node that signifies the end of the linked list
    Nil,
}

// Methods can be attached to an enum
impl List {
    // Create an empty list
    fn new() -> List {
        // `Nil` has type `List`
        Nil
    }

    // Consume a list, and return the same list with a new element at its front
    fn prepend(self, elem: uint) -> List {
        // `Cons` also has type List
        Cons(elem, box self)
    }

    // Return the length of the list
    fn len(&self) -> uint {
        // `self` has to be matched, because the behavior of this method
        // depends on the variant of `self`
        // `self` has type `&List`, and `*self` has type `List`, matching on a
        // concrete type `T` is preferred over a match on a reference `&T`
        match *self {
            // Can't take ownership of the tail, because `self` is borrowed;
            // instead take a reference to the tail
            Cons(_, ref tail) => 1 + tail.len(),
            // Base Case: An empty list has zero length
            Nil => 0
        }
    }

    // Return representation of the list as a (heap allocated) string
    fn stringify(&self) -> String {
        match *self {
            Cons(head, ref tail) => {
                // `format!` is similar to `print!`, but returns a heap
                // allocated string instead of printing to the console
                format!("{}, {}", head, tail.stringify())
            },
            Nil => {
                format!("Nil")
            },
        }
    }
}

fn main() {
    // Create an empty linked list
    let mut list = List::new();

    // Append some elements
    list = list.prepend(1);
    list = list.prepend(2);
    list = list.prepend(3);

    // Show the final state of the list
    println!("linked list has length: {}", list.len());
    println!("{}", list.stringify());
}

Saving this as a file named test.rs and compiling with rustc test.rs gives the errors:

test.rs:2:12: 2:16 error: unresolved import `List::Cons`. Cannot import from a trait or type implementation
test.rs:2 use List::{Cons, Nil};
                     ^~~~
test.rs:2:18: 2:21 error: unresolved import `List::Nil`. Cannot import from a trait or type implementation
test.rs:2 use List::{Cons, Nil};
                           ^~~
error: aborting due to 2 previous errors

Yet if you run this in the online site linked to, it works just fine. I don't understand why this isn't working for me. Do I need the latest (nightly) Rust?

rustc --version shows I've got version 0.12.0-dev.

Upvotes: 0

Views: 675

Answers (1)

BurntSushi5
BurntSushi5

Reputation: 15354

Yes, you need the nightly. Namespaced enums were introduced after 0.12 was release if I recall correctly.

In general, unless you have a really good reason not to, you should be using the nightlies. That's what most everyone uses. If you don't, you'll be fighting against the grain---most active libraries are updated regularly as nightlies are released.

Upvotes: 3

Related Questions