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 yield a thread in Rust?
- How to iterate string lines in Rust
- How to get a value by key from JSON in Rust?
- How to match whitespace with a regex in Rust?
- How do I use a variable from another file in Rust?
- How to escape dots with regex in Rust?
- How to replace strings using Rust regex?
- How to use regex to match a double quote in Rust?
- How to add matrices in Rust?
See more codes...