DEV Community

Discussion on: How to Stick with Rust

Collapse
 
brokenthorn profile image
Paul-Sebastian Manole • Edited

Oh, and "Learn the lingo" so that the compiler CAN teach you. :)

Awesome write-up. It pretty much sums up my experience as well.

And btw, you do use &str (string slices), but not where you would normally use it, thinking in ways similar to other programming languages. In Rust you have two types of Strings because of the data oriented approach (which is something you should learn if you want to get better at Rust: DOP vs OOP) + lifetimes.

One's a heap allocated string like in most languages (ie. Rust's String struct), the other is a local, stack-based string which you almost always use when dealing with strings locally, like when working with them (ie. you don't need to allocated and deallocated strings all the time as often as one might think you do with Rust).

The beginner problems with &str stem from the lifetimes of Rust types.

&str being a reference, it has an implicit lifetime in most cases. When you start working with functions returning these, you get into a bit of trouble because sometimes the compiler can't make out the correct lifetime for these references, and requires the (Rust noobie) programmer to explicitly define these lifetimes.

But then the programmer needs to understand Rust lifetime contexts... And most of us have or have had a hard time trying to understand this new concept. I believe once you learn lifetimes in Rust, the rest is easy (because you can experiment more easily without getting frustrated by the compiler's errors).

Collapse
 
pancy profile image
Pan Chasinga

Great, thanks for the elaboration!

Collapse
 
brokenthorn profile image
Paul-Sebastian Manole

You're welcome. Even though I ended up elaborating kind of a lot, it's just my 2 cents, because I'm still just a Rust newbie.