Rustのdestructure_bind構文の実験

#[derive(Debug)]
struct State<T>(T);

fn main() {
let state = State(5);
let State(value) = state;

println!("{:?}", state);
println!("{}", value);
}

実行結果

State(5)
5

6行目のように、tupleの一部を取り出して変数に割り当てることができる。


#[derive(Debug)]
struct State<T, U>{
value: T,
other: U,
}

fn main() {
let state = State{ value: 5, other: "hoge"};
let State { value: first, other: second } = state;

println!("{:?}", state);
println!("{}, {}", first, second);
}

実行結果

State { value: 5, other: "hoge" }
5, hoge

構造体でも同様。このとき

Related Articles