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 a URL with a regex in Rust?
- How to use regex captures in Rust?
- How to replace a capture group using Rust regex?
- How to print a Rust HashMap?
- How to use negation in Rust regex?
- How to ignore case in Rust regex?
- How to match the end of a line in a Rust regex?
- How to use regex lookahead in Rust?
- Regex example to match multiline string in Rust?
- How to make regex case insensitive in Rust?
See more codes...