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 perform matrix operations in Rust?
- How do you create a Rust string from a character array?
- How to map with index in Rust
- How to match whitespace with a regex in Rust?
- How to use regex lookbehind in Rust?
- Regex example to match multiline string in Rust?
- How to match a URL with a regex in Rust?
- How to use regex lookahead in Rust?
- How to replace strings using Rust regex?
- How to sort a Rust HashMap?
See more codes...