rustrust string append
Rust strings are immutable, meaning that once created, they cannot be changed. However, it is possible to append a string to an existing string using the push_str()
method.
Example code
let mut s = String::from("Hello");
s.push_str(", world!");
Output example
Hello, world!
The code above creates a String
object called s
and assigns it the value Hello
. The push_str()
method is then used to append the string , world!
to the existing string. The result is a new string with the value Hello, world!
.
The push_str()
method takes a &str
as an argument, so it is possible to append a string literal or a string slice to an existing string.
Helpful links
More of Rust
- How to match whitespace with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to split a string with Rust regex?
- How to iterate over a Rust slice with an index?
- How to use negation in Rust regex?
- How to use regex captures in Rust?
- Regex example to match multiline string in Rust?
- How to get a capture group using Rust regex?
- How to use modifiers in a Rust regex?
- How to create a HashMap of structs in Rust?
See more codes...