rustrust string build
A Rust String
is a type of data structure used to store and manipulate text. It is a growable, mutable, owned, UTF-8 encoded string type. It is a heap-allocated data structure that is stored as a pointer.
let mut s = String::new();
s.push_str("Hello");
s.push(' ');
s.push_str("world!");
println!("{}", s);
Output example
Hello world!
The code above creates a new String
called s
and then uses the push_str()
and push()
methods to add the text "Hello" and "world!" to it. Finally, it prints out the contents of the String
using the println!
macro.
The String
type has many useful methods for manipulating text, such as split()
, replace()
, trim()
, and to_lowercase()
.
Helpful links
More of Rust
- How to implement PartialEq for a Rust HashMap?
- How to use regex with bytes in Rust?
- How to parse a file with Rust regex?
- How to get a capture group using Rust regex?
- How to replace a capture group using Rust regex?
- How to use regex to match a group in Rust?
- How to match the end of a line in a Rust regex?
- How to use groups in a Rust regex?
- How to find the first match in a Rust regex?
- How to check if a regex is valid in Rust?
See more codes...