How to convert str to string in rust

convert str to string in rust

To convert a &str to a String in Rust, you can use the to_string method:

 

let str_slice: &str = "hello";
let string = str_slice.to_string();

Alternatively, you can use the String::from function to create a new String from a &str:

 

let str_slice: &str = "hello";
let string = String::from(str_slice);

 

Both of these methods create a new String object on the heap, which is owned by the Rust runtime. The original &str slice is not modified or dropped.

 

Leave a Reply

Your email address will not be published. Required fields are marked *