Enumerations

This page is under construction. Please come back later.
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#[macro_use]
mod utils;
use num_enum::IntoPrimitive;
use num_enum::TryFromPrimitive;
enum_str! {
enum CompassPoint {
North = 0,
East = 1,
South = 2,
West = 3,
}
}
fn compass_point_to_string(c:CompassPoint) -> String {
let mut result:&'static str = "";
match c {
CompassPoint::North => {
result = "North";
}
CompassPoint::East => {
result = "East";
}
CompassPoint::South => {
result = "South";
}
CompassPoint::West => {
result = "West";
}
}
return result.to_string();
}
fn turn_right(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.name());
println!("SOUTH: {}", compass_point_to_string(CompassPoint::South));
println!("turnRight(cp1): {}", compass_point_to_string(turn_right(cp1)));
let 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`.
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#[macro_use]
mod utils;
use num_enum::IntoPrimitive;
use num_enum::TryFromPrimitive;
enum_str! {
enum Color {
Red = 1,
Green = 2,
Yellow = 3,
Blue = 4,
Magenta = 5,
Cyan = 6,
}
}
fn combine_colors(c1:Color, c2:Color) -> Color {
let i1:isize = c1.into();
let i2:isize = c2.into();
let result:isize = i1 | i2;
let return_color:Color = Color::try_from(result).unwrap();
return return_color;
}
fn main() {
let mut c1:Color = Color::Blue;
println!("c1: {}", c1.name());
println!("RED: {}", Color::Red.name());
println!("combineColors(c1, RED): {}", combine_colors(c1, Color::Red).name());
let c2:Color = Color::Green;
println!("combineColors(c1, c2): {}", combine_colors(c1, c2).name());
if c1 == c2 {
println!("c1 == c2");
} else {
println!("c1 != c2");
}
c1 = c2;
if c1 == c2 {
println!("c1 == c2");
} else {
println!("c1 != c2");
}
let bad:Color = Color::try_from(7).unwrap();
println!("bad: {}", bad.name());
}
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]]
Pure Programmer


