Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions 8/8-5-2_guards.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
66 changes: 66 additions & 0 deletions 8/8-5-3_bindings.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}