rustrust string copy
A Rust String
can be copied using the clone()
method. This method creates a new String
with the same contents as the original.
Example
let s1 = String::from("Hello World!");
let s2 = s1.clone();
println!("s1 = {}, s2 = {}", s1, s2);
Output example
s1 = Hello World!, s2 = Hello World!
The clone()
method:
- Creates a new
String
with the same contents as the original. - Allocates memory for the new
String
on the heap. - Copies the contents of the original
String
into the newString
.
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...