I have this Vec (well it is a double-ended queue but for all intent and purpose, it should probably function as a vector) that I would like to alter:
let mut text_history:VecDeque<String>= VecDeque::new();
I have two options
- Pass its mutable reference:
fn append_history(text_history: &mut VecDeque<String>) {
text_history.push_back(format!("hello"));
}
- Pass and return it, catch later:
fn append_history(text_history: & VecDeque<String>) ->VecDeque<String> {
let mut new_text_history= text_history.clone();
new_text_history.push_back(format!("hello"));
return new_text_history;
}
And then
text_history=append_history(text_history);
So both work, and 1 is probably a more efficient way to do what I want but I think 2 will produce a more systematic/idiomatic code?
I am not sure. Which one do you recommend, or neither and I should go for something else?