Skip to content

Instantly share code, notes, and snippets.

@Shaun289
Last active October 28, 2021 00:56
Show Gist options
  • Save Shaun289/53004410e3ec4db49f42c136fa288395 to your computer and use it in GitHub Desktop.
Save Shaun289/53004410e3ec4db49f42c136fa288395 to your computer and use it in GitHub Desktop.
Rust study : c like enum
/*
The rust by example ko https://hanbum.gitbooks.io/rustbyexample/content/custom_types/enum/c_like.html
compiled on https://play.rust-lang.org/
result:
running 1 test
test tests::number ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
*/
// 사용하지 않는 코드 때문에 생성되는 경고를 숨기는 속성
#![allow(dead_code)]
enum Number {
Zero,
One,
Two,
}
enum Color {
Red = 0xff0000,
Green = 0x00ff00,
Blue = 0x0000ff,
}
#[cfg(test)]
mod tests
{
use super::*;
#[test]
fn number() {
assert_eq!(Number::Zero as i32, 0);
assert_eq!(Number::One as i32, 1);
assert_eq!(Number::Two as i32, 2);
}
fn color() {
assert_eq!(Color::Red as i32, 0xff0000);
assert_eq!(Color::Green as i32, 0xff00);
assert_eq!(Color::Blue as i32, 0xff);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment