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
Stringwith the same contents as the original. - Allocates memory for the new
Stringon the heap. - Copies the contents of the original
Stringinto the newString.
Helpful links
More of Rust
- How to match whitespace with a regex in Rust?
- How to use regex to match a double quote in Rust?
- How to parse JSON string in Rust?
- How to get all keys from a Rust HashMap?
- How to remove elements from a Rust slice?
- How to iterate an array with index in Rust
- How to set a dynamic size for a Rust slice?
- Pointer cast example in Rust
- How to use Unicode in a regex in Rust?
- How to replace a capture group using Rust regex?
See more codes...