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 create a HashSet from a String in Rust?
- How to convert a Rust HashMap to a BTreeMap?
- How to match a URL with a regex in Rust?
- How to use regex to match a double quote in Rust?
- How to replace a capture group using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to get a capture group using Rust regex?
- How to use regex to match a group in Rust?
- How to parse JSON string in Rust?
- How to match the end of a line in a Rust regex?
See more codes...