Not enough permissions
202307261920
Status: #idea
Tags: Rust
Not enough permissions
fn stringify_name_with_title(name: &Vec<String>) -> String {
name.pushfrom("Esq.");
let full = name.join(" ");
full
}
Hacky workaround: Make vector mutable
fn stringify_name_with_title(name: &mut Vec<String>) -> String {
name.pushfrom("Esq.");
let full = name.join(" ");
full
}
- This is a bad solution. See warning below
Attention
Functions should not mutate their inputs, if the user does not expect it.
Take ownership of the vector
fn stringify_name_with_title(name: Vec<String>) -> String {
name.pushfrom("Esq.");
let full = name.join(" ");
full
}
- This solution is against best-practices
- It is very rare that Rust functions take ownership of heap-owning data structures
Modify the function body
Clone the input vector
fn stringify_name_with_title(name: &Vec<String>) -> String {
let mut name_copy = name.clone();
name_copy.push("Esq.");
let full = name.join(" ");
full;
}
- This method uses
additional space and additional time, for the clone, making it inefficient
Add title to output string
fn stringify_name_with_title(name: &Vec<String>) -> String {
let mut full = name.join(" ");
full.push_str(" Esq.");
full
}