Optional and Result for optional parameters and functions that may fail.
This commit is contained in:
parent
d5427c96ca
commit
6e0e031b12
24
src/main.rs
24
src/main.rs
@ -1,21 +1,15 @@
|
||||
enum Color {
|
||||
RGB(f32, f32, f32),
|
||||
RGBA(f32, f32, f32, f32),
|
||||
CMYK(f32, f32, f32, f32),
|
||||
YUV(f32, f32, f32),
|
||||
}
|
||||
|
||||
fn color_to_string(color: Color) -> String {
|
||||
match color {
|
||||
Color::RGB(red, green, blue) => String::from("RGB"),
|
||||
Color::RGBA(red, green, blue, alpha) => String::from("RGBA"),
|
||||
Color::CMYK(cyan, magenta, yellow, key) => String::from("CMYK"),
|
||||
Color::YUV(y, u, v) => String::from("YUV"),
|
||||
fn add_10(value: Option<u32>) -> Result<u32, String> {
|
||||
match value {
|
||||
Some(v) => Ok(v + 10),
|
||||
None => Err(String::from("value was empty")),
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let color = Color::RGB(1.0, 0.0, 0.0);
|
||||
let value = add_10(Some(23));
|
||||
|
||||
println!("Color: {}", color_to_string(color));
|
||||
match value {
|
||||
Ok(v) => println!("Result was: {}", v),
|
||||
Err(e) => println!("Operatation failed. Reason was: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user