Enumerations
This page is under construction. Please come back later.
mod utils; enum CompassPoint { NORTH, EAST, SOUTH, WEST } fn CompassPointToString(c:CompassPoint) -> String { let mut result:&str = ""; match c { CompassPoint::NORTH => { result = "North"; } CompassPoint::EAST => { result = "East"; } CompassPoint::SOUTH => { result = "South"; } CompassPoint::WEST => { result = "West"; } } return result; } fn turnRight(c:CompassPoint) -> CompassPoint { if c == CompassPoint::NORTH { return CompassPoint::EAST; } else if c == CompassPoint::EAST { return CompassPoint::SOUTH; } else if c == CompassPoint::SOUTH { return CompassPoint::WEST; } else { return CompassPoint::NORTH; } } fn main() { let mut cp1:CompassPoint = CompassPoint::NORTH; println!("cp1: {}", cp1); println!("SOUTH: {}", CompassPointToString(CompassPoint::SOUTH)); println!("turnRight(cp1): {}", CompassPointToString(turnRight(cp1))); let mut cp2:CompassPoint = CompassPoint::EAST; if cp1 == cp2 { println!("cp1 == cp2"); } else { println!("cp1 != cp2"); } cp1 = cp2; if cp1 == cp2 { println!("cp1 == cp2"); } else { println!("cp1 != cp2"); } }
Output
$ rustc Enums1.rs
error[E0308]: mismatched types
--> Enums1.rs:26:9
|
10 | fn CompassPointToString(c:CompassPoint) -> String {
| ------ expected `String` because of return type
...
26 | return result;
| ^^^^^^- help: try using a conversion method: `.to_string()`
| |
| expected `String`, found `&str`
error[E0369]: binary operation `==` cannot be applied to type `CompassPoint`
--> Enums1.rs:30:7
|
30 | if c == CompassPoint::NORTH {
| - ^^ ------------------- CompassPoint
| |
| CompassPoint
|
note: an implementation of `PartialEq` might be missing for `CompassPoint`
--> Enums1.rs:3:1
|
3 | enum CompassPoint {
| ^^^^^^^^^^^^^^^^^ must implement `PartialEq`
help: consider annotating `CompassPoint` with `#[derive(PartialEq)]`
|
3 + #[derive(PartialEq)]
4 | enum CompassPoint {
|
error[E0369]: binary operation `==` cannot be applied to type `CompassPoint`
--> Enums1.rs:32:14
|
32 | } else if c == CompassPoint::EAST {
| - ^^ ------------------ CompassPoint
| |
| CompassPoint
|
note: an implementation of `PartialEq` might be missing for `CompassPoint`
--> Enums1.rs:3:1
|
3 | enum CompassPoint {
| ^^^^^^^^^^^^^^^^^ must implement `PartialEq`
help: consider annotating `CompassPoint` with `#[derive(PartialEq)]`
|
3 + #[derive(PartialEq)]
4 | enum CompassPoint {
|
error[E0369]: binary operation `==` cannot be applied to type `CompassPoint`
--> Enums1.rs:34:14
|
34 | } else if c == CompassPoint::SOUTH {
| - ^^ ------------------- CompassPoint
| |
| CompassPoint
|
note: an implementation of `PartialEq` might be missing for `CompassPoint`
--> Enums1.rs:3:1
|
3 | enum CompassPoint {
| ^^^^^^^^^^^^^^^^^ must implement `PartialEq`
help: consider annotating `CompassPoint` with `#[derive(PartialEq)]`
|
3 + #[derive(PartialEq)]
4 | enum CompassPoint {
|
error[E0277]: `CompassPoint` doesn't implement `std::fmt::Display`
--> Enums1.rs:43:22
|
43 | println!("cp1: {}", cp1);
| ^^^ `CompassPoint` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `CompassPoint`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0369]: binary operation `==` cannot be applied to type `CompassPoint`
--> Enums1.rs:47:9
|
47 | if cp1 == cp2 {
| --- ^^ --- CompassPoint
| |
| CompassPoint
|
note: an implementation of `PartialEq` might be missing for `CompassPoint`
--> Enums1.rs:3:1
|
3 | enum CompassPoint {
| ^^^^^^^^^^^^^^^^^ must implement `PartialEq`
help: consider annotating `CompassPoint` with `#[derive(PartialEq)]`
|
3 + #[derive(PartialEq)]
4 | enum CompassPoint {
|
error[E0369]: binary operation `==` cannot be applied to type `CompassPoint`
--> Enums1.rs:53:9
|
53 | if cp1 == cp2 {
| --- ^^ --- CompassPoint
| |
| CompassPoint
|
note: an implementation of `PartialEq` might be missing for `CompassPoint`
--> Enums1.rs:3:1
|
3 | enum CompassPoint {
| ^^^^^^^^^^^^^^^^^ must implement `PartialEq`
help: consider annotating `CompassPoint` with `#[derive(PartialEq)]`
|
3 + #[derive(PartialEq)]
4 | enum CompassPoint {
|
error: aborting due to 7 previous errors
Some errors have detailed explanations: E0277, E0308, E0369.
For more information about an error, try `rustc --explain E0277`.
mod utils; enum Color { RED = 1, GREEN = 2, YELLOW = 3, BLUE = 4, MAGENTA = 5, CYAN = 6 } fn combineColors(c1:Color, c2:Color) -> Color { let i1:isize = enumToInt(c1); let i2:isize = enumToInt(c2); let result:isize = i1 | i2; let returnColor:Color = intToEnum("Color",result); return returnColor; } fn main() { let mut c1:Color = Color::BLUE; println!("c1: {}", c1); println!("RED: {}", Color::RED); println!("combineColors(c1, RED): {}", combineColors(c1, Color::RED)); let mut c2:Color = Color::GREEN; println!("combineColors(c1, c2): {}", combineColors(c1, c2)); if c1 == c2 { println!("c1 == c2"); } else { println!("c1 != c2"); } c1 = c2; if c1 == c2 { println!("c1 == c2"); } else { println!("c1 != c2"); } let mut bad:Color = intToEnum(Color,7); println!("bad: {}", bad); }
Output
$ rustc Enums2.rs
error[E0423]: expected value, found enum `Color`
--> Enums2.rs:38:32
|
38 | let mut bad:Color = intToEnum(Color,7);
| ^^^^^
|
note: the enum is defined here
--> Enums2.rs:3:1
|
3 | / enum Color {
4 | | RED = 1,
5 | | GREEN = 2,
6 | | YELLOW = 3,
... |
9 | | CYAN = 6
10 | | }
| |_^
help: you might have meant to use one of the following enum variants
|
38 | let mut bad:Color = intToEnum(Color::BLUE,7);
| ~~~~~~~~~~~
38 | let mut bad:Color = intToEnum(Color::CYAN,7);
| ~~~~~~~~~~~
38 | let mut bad:Color = intToEnum(Color::GREEN,7);
| ~~~~~~~~~~~~
38 | let mut bad:Color = intToEnum(Color::MAGENTA,7);
| ~~~~~~~~~~~~~~
and 2 other candidates
error[E0425]: cannot find function `enumToInt` in this scope
--> Enums2.rs:13:17
|
13 | let i1:isize = enumToInt(c1);
| ^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `enumToInt` in this scope
--> Enums2.rs:14:17
|
14 | let i2:isize = enumToInt(c2);
| ^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `intToEnum` in this scope
--> Enums2.rs:16:26
|
16 | let returnColor:Color = intToEnum("Color",result);
| ^^^^^^^^^ not found in this scope
error[E0277]: `Color` doesn't implement `std::fmt::Display`
--> Enums2.rs:22:21
|
22 | println!("c1: {}", c1);
| ^^ `Color` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Color`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: `Color` doesn't implement `std::fmt::Display`
--> Enums2.rs:23:22
|
23 | println!("RED: {}", Color::RED);
| ^^^^^^^^^^ `Color` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Color`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: `Color` doesn't implement `std::fmt::Display`
--> Enums2.rs:24:41
|
24 | println!("combineColors(c1, RED): {}", combineColors(c1, Color::RED));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Color` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Color`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: `Color` doesn't implement `std::fmt::Display`
--> Enums2.rs:26:40
|
26 | println!("combineColors(c1, c2): {}", combineColors(c1, c2));
| ^^^^^^^^^^^^^^^^^^^^^ `Color` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Color`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0369]: binary operation `==` cannot be applied to type `Color`
--> Enums2.rs:27:8
|
27 | if c1 == c2 {
| -- ^^ -- Color
| |
| Color
|
note: an implementation of `PartialEq` might be missing for `Color`
--> Enums2.rs:3:1
|
3 | enum Color {
| ^^^^^^^^^^ must implement `PartialEq`
help: consider annotating `Color` with `#[derive(PartialEq)]`
|
3 + #[derive(PartialEq)]
4 | enum Color {
|
error[E0369]: binary operation `==` cannot be applied to type `Color`
--> Enums2.rs:33:8
|
33 | if c1 == c2 {
| -- ^^ -- Color
| |
| Color
|
note: an implementation of `PartialEq` might be missing for `Color`
--> Enums2.rs:3:1
|
3 | enum Color {
| ^^^^^^^^^^ must implement `PartialEq`
help: consider annotating `Color` with `#[derive(PartialEq)]`
|
3 + #[derive(PartialEq)]
4 | enum Color {
|
error[E0425]: cannot find function `intToEnum` in this scope
--> Enums2.rs:38:22
|
38 | let mut bad:Color = intToEnum(Color,7);
| ^^^^^^^^^ not found in this scope
error: aborting due to 11 previous errors
Some errors have detailed explanations: E0277, E0369, E0423, E0425.
For more information about an error, try `rustc --explain E0277`.
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Rust Language Reference]]
- [[Rust Compiler]]