Rust by Example - страница 20
>
>HSL(u32, u32, u32),
>CMY(u32, u32, u32),
>CMYK(u32, u32, u32, u32),
>}
>fn main() {
>let color = Color::RGB(122, 17, 40);
>// TODO ^ Try different variants for `color`
>println!("What color is it?");
>// An `enum` can be destructured using a `match`.
>match color {
>Color::Red => println!("The color is Red!"),
>Color::Blue => println!("The color is Blue!"),
>Color::Green => println!("The color is Green!"),
>Color::RGB(r, g, b) =>
>println!("Red: {}, green: {}, and blue: {}!", r, g, b),
>Color::HSV(h, s, v) =>
>println!("Hue: {}, saturation: {}, value: {}!", h, s, v),
>Color::HSL(h, s, l) =>
>println!("Hue: {}, saturation: {}, lightness: {}!", h, s, l),
>Color::CMY(c, m, y) =>
>println!("Cyan: {}, magenta: {}, yellow: {}!", c, m, y),
>Color::CMYK(c, m, y, k) =>
>println!("Cyan: {}, magenta: {}, yellow: {}, key (black): {}!",
>c, m, y, k),
>// Don't need another arm because all variants have been examined
>}
>}
>הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
For pointers, a distinction needs to be made between destructuring and dereferencing as they are different concepts which are used differently from a language like C.
• Dereferencing uses *
• Destructuring uses &, ref, and ref mut
>fn main() {
>// Assign a reference of type `i32`. The `&` signifies there
>// is a reference being assigned.
>let reference = &4;
>match reference {
>// If `reference` is pattern matched against `&val`, it results
>// in a comparison like:
>// `&i32`
>// `&val`
>// ^ We see that if the matching `&`s are dropped, then the `i32`
>// should be assigned to `val`.
>&val => println!("Got a value via destructuring: {:?}", val),
>}
>// To avoid the `&`, you dereference before matching.
>match *reference {
>val => println!("Got a value via dereferencing: {:?}", val),
>}
>// What if you don't start with a reference? `reference` was a `&`
>// because the right side was already a reference. This is not
>// a reference because the right side is not one.
>let _not_a_reference = 3;
>// Rust provides `ref` for exactly this purpose. It modifies the
>// assignment so that a reference is created for the element; this
>// reference is assigned.
>let ref _is_a_reference = 3;
>// Accordingly, by defining 2 values without references, references
>// can be retrieved via `ref` and `ref mut`.
>let value = 5;
>let mut mut_value = 6;
>// Use `ref` keyword to create a reference.
>match value {
>ref r => println!("Got a reference to a value: {:?}", r),
>}
>// Use `ref mut` similarly.
>match mut_value {
>ref mut m => {
>// Got a reference. Gotta dereference it before we can
>// add anything to it.
>*m += 10;
>println!("We added 10. `mut_value`: {:?}", m);
>},
>}
>}
>הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Similarly, a struct can be destructured as shown:
>fn main() {
>struct Foo {
>x: (u32, u32),
>y: u32,
>}
>// Try changing the values in the struct to see what happens
>let foo = Foo { x: (1, 2), y: 3 };
>match foo {
>Foo { x: (1, b), y } => println!("First of x is 1, b = {}, y = {} ", b, y),
>// you can destructure structs and rename the variables,
>// the order is not important
>Foo { y: 2, x: i } => println!("y is 2, i = {:?}", i),