rustHow do I append a character to a string in Rust?
You can append a character to a string in Rust using the push()
method. This method takes a single character as an argument and adds it to the end of the string.
let mut s = String::from("Hello");
s.push('!');
println!("{}", s);
Output example
Hello!
The code above does the following:
- Declares a mutable string
s
and assigns it the valueHello
. - Calls the
push()
method ons
with the argument!
. - Prints the value of
s
to the console.
Helpful links
More of Rust
- How to parse JSON string in Rust?
- How to convert JSON to a struct in Rust?
- How to use a custom hash function with a Rust HashMap?
- Hashshet example in Rust
- How to split a string with Rust regex?
- How to compile a regex in Rust?
- How to convert the keys of a Rust HashMap to a vector?
- How to initialize a Rust HashMap?
- How to yield return in Rust?
- How to modify an existing entry in a Rust HashMap?
See more codes...