我想在VecDeque中存储值,并在它更新值之后。
最小示例:
phoneNumber / email
错误:
use std::collections::VecDeque;
fn main() {
let mut v = VecDeque::new();
let mut str1 = String::from("Hello");
v.push_back(str1);
let str = String::from(" World");
str1.push_str(&str);
}
问题:
如何将项目添加到集合中并在更新此项目之后?
答案 0 :(得分:1)
使用back_mut
v.back_mut().unwrap().push_str(&str);
或仅将参考存储在双端队列中
v.push_back(&mut str1);
答案 1 :(得分:0)
我的工作示例:
use std::collections::VecDeque;
fn main() {
let mut v = VecDeque::new();
let mut str1 = String::from("Hello");
v.push_back(str1);
let mut str2 = String::from(" World");
match v.back_mut() {
Some(value) => value.push_str(&str2),
None => println!("Error")
}
match v.back() {
Some(value) => println!("{}", value),
None => println!("Error2")
}
}