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
sand assigns it the valueHello. - Calls the
push()method onswith the argument!. - Prints the value of
sto the console.
Helpful links
More of Rust
- How to print a Rust HashMap?
- How to add an entry to a Rust HashMap?
- How to match the end of a line in a Rust regex?
- How to map a Rust slice?
- Regex example to match multiline string in Rust?
- How to use regex captures in Rust?
- How to replace all using regex in Rust?
- How to use binary regex in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to create a HashMap of structs in Rust?
See more codes...