From 6b6051e5e41af993022a709cf704868f730809e7 Mon Sep 17 00:00:00 2001 From: asada_n Date: Wed, 15 Dec 2021 18:26:36 +0900 Subject: [PATCH] 8-5-2. Guards and 8-5-3. Bindings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 8-5-2. ガード * 8-5-3. バインディング --- 8/8-5-2_guards.rs | 37 +++++++++++++++++++++++++ 8/8-5-3_bindings.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 8/8-5-2_guards.rs create mode 100644 8/8-5-3_bindings.rs diff --git a/8/8-5-2_guards.rs b/8/8-5-2_guards.rs new file mode 100644 index 0000000..ba664cd --- /dev/null +++ b/8/8-5-2_guards.rs @@ -0,0 +1,37 @@ +fn guard_example1(pair: (i32, i32)) { + println!("Tell me about {:?}", pair); + match pair { + (x, y) if x == y => println!("These are twins"), + // The ^ `if condition` part is a guard + // ^ `if`とそれに続く条件式がガードです。 + (x, y) if x + y == 0 => println!("Antimatter, kaboom!"), + (x, _) if x % 2 == 1 => println!("The first one is odd"), + _ => println!("No correlation..."), + } +} + +#[derive(Clone, Copy, Debug)] +struct A { + x: i32, +} + +fn main() { + let pair = (2, -2); + // TODO ^ Try different values for `pair` + // TODO ^ `pair`の値を変更してみましょう。 + guard_example1(pair); + + // When a expression matches multiple guards, + // the expression is captured by the topmost guard. + let pair = (0, 0); + guard_example1(pair); // Satisfies `x == y` and `x + y == 0` but prints 'There are twins'. + + // The compiler cannot check the match expression covers all possible conditions anymore, + // so you MUST use the `_` pattern at the end. + let number: u8 = 4; + match number { + i if i == 0 => println!("Zero"), + i if i > 0 => println!("Greater than zero"), + _ => panic!("Fell through"), // This should not be possible to reach + } +} diff --git a/8/8-5-3_bindings.rs b/8/8-5-3_bindings.rs new file mode 100644 index 0000000..064787e --- /dev/null +++ b/8/8-5-3_bindings.rs @@ -0,0 +1,66 @@ +fn binding_example1(age: u32) { + match age { + 0 => println!("I haven't celebrated my first birthday yet"), + // Could `match` 1 ..= 12 directly but then what age + // would the child be? Instead, bind to `n` for the + // sequence of 1 ..= 12. Now the age can be reported. + // `1 ... 12`の値を一挙に`match`させることができる。 + // しかしその場合、子供は正確には何歳? + // マッチした値を`n`にバインディングすることで値を使用できる。 + n @ 1 ..= 12 => println!("I'm a child of age {:?}", n), + n @ 13 ..= 19 => println!("I'm a teen of age {:?}", n), + // Nothing bound. Return the result. + // マッチしなかった場合の処理 + n => println!("I'm an old person of age {:?}", n), + } +} + +#[derive(Debug)] +struct Movable { x: i32 } + +#[derive(Clone, Copy, Debug)] +struct Copyable { x: i32 } + +fn main() { + println!("Tell me what type of person you are"); + binding_example1(8); // Prints `I'm a child of age 8`. + binding_example1(15); // Prints `I'm a teen of age 15`. + binding_example1(30); // Prints `I'm an old person of age 30`. + + // Capturing by move + let a = Movable { x: 100 }; + println!("a: {:p}", &a); + println!("x: {:p}", &a.x); // &a equals to &a.x, because `x` is the first member of `a`. + match a { + b @ Movable { x: y } => { + println!("Moved a: {:p}", &b); + println!("Captured a.x: {:p}", &y); // &b not equals to &y, because `y` is newly captured variable. + // println!("Original a: {:p}", &a); + // ^^ value is borrowed after move + } + } + + // Capturing by copy + let a = Copyable { x: 100 }; + println!("a: {:p}", &a); + println!("x: {:p}", &a.x); + match a { + b @ Copyable { x: y } => { + println!("Copied a: {:p}", &b); + println!("Captured a.x: {:p}", &y); // &b not equals to &y, because `y` is newly captured variable. + println!("Original a: {:p}", &a); // `a` can be borrowed. + } + } + + // Capturing by reference + let a = Movable { x: 100 }; + println!("a: {:p}", &a); + println!("x: {:p}", &a.x); + match a { + ref b @ Movable { x: ref y } => { + println!("Referenced a: {:p}", b); + println!(" a.x: {:p}", y); // &b equals to &y, because `y` captures a reference of a.x. + println!("Original a: {:p}", &a); + } + } +} \ No newline at end of file