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 {
|
fn add_10(value: Option<u32>) -> Result<u32, String> {
|
||||||
RGB(f32, f32, f32),
|
match value {
|
||||||
RGBA(f32, f32, f32, f32),
|
Some(v) => Ok(v + 10),
|
||||||
CMYK(f32, f32, f32, f32),
|
None => Err(String::from("value was empty")),
|
||||||
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 main() {
|
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