To remove a character at a specific index in a string in Rust, you can use string slicing.
In this example,
str1
with a value of "Hello World"
.fn main() {
let mut str1 = String::from("Hello World");
let index_to_remove = 6;
str1.remove(index_to_remove);
println!("Modified string is: {}", str1);
}
Modified string is: Hello orld
In this example,
str2
with a value of "apple"
.fn main() {
let mut str2 = String::from("apple");
let index_to_remove = 2;
str2.remove(index_to_remove);
println!("Modified string is: {}", str2);
}
Modified string is: aple
In this tutorial, we learned How to Remove Character at specific Index in a String in Rust language with well detailed examples.