私は何をしようとしている同じベクトルにそれをプッシュする:私はlast()
を呼び出すときに、それが返されることを理解しベクトルの最後の要素を取得し、
enum Test {
Value1,
Value2,
Value3
}
fn main() {
let mut test_vec: Vec<Test> = Vec::new();
test_vec.push(Test::Value2);
if let Some(last) = test_vec.last() {
test_vec.push(*last);
}
//Wanted output: vector with [Test::Value2, Test::Value2]
}
をOption<&Test>
だから、test_vec
を借りますif-letブロックの終わりまで。
私は成功せず、次のことを試してみました:
if let Some(last) = test_vec.last().map(|v| v.clone()) {
test_vec.push(*last);
}
//and
let last = test_vec.last().unwrap().clone();
test_vec.push(*last);