rustrust string clone
String::clone
is a method in Rust that creates a new String
from an existing one. It is used to create a deep copy of a String
so that the original String
can be modified without affecting the new one.
Example
let s1 = String::from("Hello");
let s2 = s1.clone();
println!("s1 = {}, s2 = {}", s1, s2);
Output example
s1 = Hello, s2 = Hello
The code above creates two String
s, s1
and s2
, from the same string literal. s2
is created using the clone
method on s1
. This creates a deep copy of s1
so that modifying s1
does not affect s2
.
Parts of the code:
let s1 = String::from("Hello");
: This creates aString
from the string literal "Hello".let s2 = s1.clone();
: This creates a deep copy ofs1
and assigns it tos2
.println!("s1 = {}, s2 = {}", s1, s2);
: This prints out the values ofs1
ands2
.
Helpful links
More of Rust
- How to use regex to match a group in Rust?
- How to use regex to match a double quote in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to use regex with bytes in Rust?
- How to parse JSON string in Rust?
- How to match the end of a line in a Rust regex?
- How to match whitespace with a regex in Rust?
- How to split a string by regex in Rust?
- Regex example to match multiline string in Rust?
- Hashshet example in Rust
See more codes...