8 deadly mistakes beginner Rust developers make

preview_player
Показать описание
New Rustaceans are an inquisitive bunch and want to dive into the language head first, but sometimes this leads to some common mistakes that beginners make. Today I am going to discuss 8 common mistakes that are made by new Rust developers or even more experienced Rust developers alike.

Chapters:
0:00 Intro
0:32 Mistake 1
1:11 Mistake 2
1:55 Mistake 3
2:40 Mistake 4
3:37 Mistake 5
4:50 Mistake 6
6:47 Mistake 7
7:48 Mistake 8
9:05 Bonus
Рекомендации по теме
Комментарии
Автор

3:15 when just returning true/false from match arms, it can be written even more concise using the matches! macro:

fn can_publish_blog(role: Role) -> bool {
matches!(role, Role::Admin | Role::Writer)
}

Possseidon
Автор

1:28 For mistake #2, there is a common mistake within it: *Overusing vectors* . A lot of times, beginners will prefer creating vectors whenever they need an arbitrary amount of things. This is indeed what vectors are for, _when they are useful_ .

Vectors are relatively costly and are meant for _storing_ data in a longer timespan. If you are collecting data into a vector just to perform operations on the vector and then no longer need it thereafter, then you are doing it wrong. The fix here is to not use a vector at all.

In your minimal example, the full scale is not seen, so what you did _might_ have been necessary, but if you just wanted to use the new vector to do something with each element and then discard the vector, then all you needed to do is for-loop through the same iterator but without the `collect` method, which avoids creating a vector.

If instead you do need a vector because some other function requires it or for some other reason, then you should look into why you have a vector in the first place. There would be no need to have a vector twice in your code if one vector transforms into the other. The fix in this case is to look for how to avoid creating the first vector, and just using an iterator like in my previous fix to collect into the desired vector directly. If you got the first vector from a function, then shame on that function; it should have returned an iterator instead of allocating a vector. If you created that function, then here's what you can do to fix the return type:

YanVidz
Автор

Naturally the advanced error is object oriented mutability, with the incorrect solution being to slop more mutability onto the code until it complies and the correct way being to just make it more functional with better separation of concerns

orterves
Автор

Common mistake number 0: Waiting too long to start learning Rust.

thingsiplay
Автор

Rust has opened up my eyes to the dangers of inner mutibility -- and holding references to other objects in general. I have had to unlearn some seriously bad habits I picked up from languages where this is easy.

TheEvertw
Автор

If you're going to turn warnings into errors in CI, you should also pin the Rust toolchain version used, otherwise new warnings can be added in any minor version (especially in Clippy). Almost all of my projects hit a new clippy finding after every couple of Rust updates, if they hard-blocked CI then it would be a huge nuisance.

Fun fact: You can get out ahead of some of these checks by installing the nightly toolchain and running cargo +nightly clippy --all-targets. It's like those "new lint from the future just leaked" memes except real.

FiniteSteakMachine
Автор

The community needs more of these friendly newbie resources! Keep it up!

paulgupta
Автор

The last one is extremely helpful. It's often hard to wrap up my mind around this inability of porting code line by line.

naughtrussel
Автор

I wrote an interpreter as my first project in Rust, and I encountered that last problem exactly. I had closures that would have a reference to the enclosing scope, and the borrow checker was very upset with me. I tried to fix it using lifetime annotations, but I quickly got way too confused. I managed to get it to work by slapping Rc and RefCell on things, but it felt really wrong. Thanks for sharing ideas about how to avoid these types of issues in the future!

MegaJambo
Автор

Nice video! However, I disagree with its thumbnail. There's nothing bad with `Rc<RefCell<T>>` on its own, thumbnail is misleading. I'd instead put for loop with i += 1 and array indexing, a more common "mistake" due to poor knowledge of the lang.

zohnannor
Автор

While I agree that excessive usage of `Rc<RefCell<T>>` is a sign of bad design you would really need to work hard to cause a deadlock. And there is no decrease in memory safety. Unless you touch `unsafe`, your code **will** be memory safe.

aleksanderkrauze
Автор

Love mistake 6 as I did this and didn't use some of the amazing built-in traits. One I recommend looking at is the `Display` trait to customise the output of a println! or format! marcos.

GeekMasher
Автор

11:05 monsters could be better-defined as:
“let mut monsters = vec![Monster::default(); 5];”

addisonmigash
Автор

One fatal mistake most make is not using Rust.

twentyeightO
Автор

Most of these were pretty avoidable when I started learning rust. Maybe it's because I came from a functional programming background; understanding the value of enums, results and option, using map, filter, reduce etc. Now that being said, the biggest issue I had with rust when I first started is that I treated it like a functional language. Rust is first and foremost a procedural and imperative language with functional features. If you try to treat it as a purely functional language you're going to be in for a lot of pain. I can't tell you how often I bounced off the borrow checker because of this. Anyway, good video; all this is really good advice and it's hard to remember that this stuff is not obvious when you've been using the language for 8 years.

draakisback
Автор

I like the javascript to rust example, it made things more easier to understand. Could you do more of them if most people find it useful :) its sure as hell useful to me.

thebarnowlsmusic
Автор

Was not aware of the array_windows and windows methods on arrays. Might start using this more often... although right now, array_windows is only in nightly, so the windows method may be preferable.

KiranasOfRizon
Автор

keep up the fantastic rust videos. they are always helpful and very intresting. one of the reasons why i love rust

andredasilva
Автор

You know, it would be really helpful to have a video expounding on each of these points, as it was hard to understand exactly what I need to learn/change in my programming from such short examples - however, it was also apparent from the examples that there are nuances I'm missing with my Rust programming.

letsgobrandon
Автор

Mistake 2: overusing slice indexing
6:37: Making actual mistake when using slice indexing

Morgan_iv