Reputation: 1401
Following the Rust guide, I wanted to run the example from section 8 and 9.
enum MOrdering {
MLess,
MEqual,
MGreater,
}
fn cmp(a: int, b: int) -> MOrdering {
if a < b { MLess }
else if a > b { MGreater }
else { MEqual }
}
fn main() {
let x = 5i;
let y = 3i;
match cmp(x, y) {
MLess => println!("less"),
MGreater => println!("greater"),
MEqual => println!("equal"),
}
}
This does not compile and it appears to be the same as the example with just the enum renamed to make sure it didn't conflict with core::cmp::Ordering.
These are the errors I get while compiling:
rustc src/main.rs
src/main.rs:8:16: 8:21 error: unresolved name `MLess`. Did you mean `a`?
src/main.rs:8 if a < b { MLess }
^~~~~
src/main.rs:9:21: 9:29 error: unresolved name `MGreater`.
src/main.rs:9 else if a > b { MGreater }
^~~~~~~~
src/main.rs:10:12: 10:18 error: unresolved name `MEqual`. Did you mean `a`?
src/main.rs:10 else { MEqual }
^~~~~~
error: aborting due to 3 previous errors
What am I missing that the guide left out? On Rust 0.13.
rustc 0.13.0-nightly (fac5a0767 2014-11-26 22:37:06 +0000)
Thank you for your help.
Upvotes: 1
Views: 205
Reputation: 128031
This is not a bug; a little time ago an RFC on enum variants scoping has been implemented. Now enum variants are scoped under their respective enums, so you need to write this:
enum MOrdering {
MLess,
MEqual,
MGreater,
}
fn cmp(a: int, b: int) -> MOrdering {
if a < b { MOrdering::MLess }
else if a > b { MOrdering::MGreater }
else { MOrdering::MEqual }
}
fn main() {
let x = 5i;
let y = 3i;
match cmp(x, y) {
MOrdering::MLess => println!("less"),
MOrdering::MGreater => println!("greater"),
MOrdering::MEqual => println!("equal"),
}
}
You can revert to the old behavior by using glob imports:
#![feature(globs)]
use MOrdering::*;
enum MOrdering {
MLess,
MEqual,
MGreater,
}
fn cmp(a: int, b: int) -> MOrdering {
if a < b { MLess }
else if a > b { MGreater }
else { MEqual }
}
fn main() {
let x = 5i;
let y = 3i;
match cmp(x, y) {
MLess => println!("less"),
MGreater => println!("greater"),
MEqual => println!("equal"),
}
}
(note: soon enabling feature flag for globs will be unnecessary because globs are planned for stabilization for 1.0)
Upvotes: 5