I've written function to push new items or update under given index, can I make it somehow better?
fn insert_or_update<'a>(arr: &mut Vec<&'a str>, idx: usize, val: &'a str) {
match arr.get(idx) {
Some(_) => std::mem::replace(&mut arr[idx], val),
None => {
arr.push(val);
""
},
};
}
fn main() {
let mut arr = vec!["a", "b", "c"];
insert_or_update(&mut arr, 0, "replaced");
println!("replaced {:?}", &arr);
insert_or_update(&mut arr, 99, "inserted");
println!("inserted {:?}", &arr);
}
I don't like that I match Some(_)
and than replacing the value, also the expected returning type is string so when there is no such item it returns ""
but this looks clutter to me