Can strip_suffix mutate a String value?

I want to remove " &" from the end of a String value. If the String doesn't contain " &", I just want the original String/I want nothing to be done to the original String. The strip_suffix method from &str does part of that, but it obviously doesn't mutate the provided String. What is the easiest way to strip a certain suffix from a mutable String? Right now, I am stuck with

		let old_str = String::from("hello world");
		let new_str = match old_str.strip_suffix(" &") {
			Some(stripped) => stripped.to_owned(),
			None => old_str,

if string.ends_with(suffix) {
    string.truncate(string.len() - suffix.len());

You could make it a method with a utility trait.


Thanks! Is there a reason a strip_suffix method doesn't exist for mutable Strings directly? It would be pretty useful.

It would need to have a distinct name, but other than that, I don't know why it doesn't exist.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.