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
}
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
}

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;
}

Add title to output string

fn stringify_name_with_title(name: &Vec<String>) -> String {
	let mut full = name.join(" ");
	full.push_str(" Esq.");
	full
}

References

  1. https://rust-book.cs.brown.edu/ch04-03-fixing-ownership-errors.html#fixing-an-unsafe-program-not-enough-permissions
  2. Fixing Ownership Errors