Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Implications of call as_str() to create new String
Hi folks,
I am trying to figure out the best way to replicate a string.
I have two methods, listed below.
Method 1 creates a new String by calling the as_str()
function. Using method 1, am I correct in thinking that a string slice with a static lifetime will be created? If so, is s2 now dependant on s1 which lives on the heap?
Method 2 creates a new String by calling the clone()
function. Using method 2, am I correct in thinking that s1
can go out of scope and it will have no effect on s2
?
// Method 1
let s1 = String::from("hello, world");
let s2 = String::from(s1.as_str());
// Method 2
let s1 = String::from("hello, world");
let s2 = s1.clone();
Any guidance would be greatly appreciated.
6 posts - 4 participants
🏷️ rust_feed