[Solved] Difference between &str and String

Hi there !

I'm new to Rust, and there is something that I'm not able to think about yet.

This value : "abc" has the type &str. When I do "abc".to_owned() or String::new("abc"), I convert &str to String.

How I've understood, that means that &str it's a full array of unicode char, so it doesn't have fixed size (the Sized trait in Rust if I've well understood), and String it's a pointer to heap allocated &str : so fixed size.

Now, when I do &s with s is a String value, I get a &str. Why ?

I see String as box with str inself. And when we take reference, we pick the value out from the box ? It means senseless for me, so there must be something I misunderstood.

I've given a detailed description of the difference between str, &str, and String before

1 Like

Incredible way of thinking. thanks a lot !

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.