To remove the last character of a string in Rust, you can use the pop method to remove the last character and return it.
In this example,
str1
with a value of "Hello World"
.fn main() {
let mut str1 = String::from("Hello World");
str1.pop();
println!("Modified string is: {}", str1);
}
Modified string is: Hello Worl
In this example,
str2
with a value of "apple"
.fn main() {
let mut str2 = String::from("apple");
str2.pop();
println!("Modified string is: {}", str2);
}
Modified string is: appl
In this tutorial, we learned How to Remove Last Character in a String in Rust language with well detailed examples.