To append a character to the beginning of a string in Rust, you can use the `+` operator for concatenation or the `insert_str()` method of the `String` type.
In this example,
str1
with a value of "ello"
.+
operator to prepend the character 'H'
to str1
.let mut str1 = String::from("ello");
str1 = "H".to_string() + &str1;
println!("{}", str1);
Hello
In this example,
str2
with a value of "orld"
.insert_str()
method to insert the character 'W'
at index 0 of str2
.let mut str2 = String::from("orld");
str2.insert_str(0, "W");
println!("{}", str2);
World
In this tutorial, we learned How to Append a Character to beginning of a String in Rust language with well detailed examples.