13 easy Rust tips (code review)

  Рет қаралды 19,880

strager

strager

Күн бұрын

Пікірлер: 39
@fennewald5230
@fennewald5230 2 жыл бұрын
For the bit options, seems like the perfect case for enumSet, which does all the fancy bit operations in a really easy-to-use way
@sacredindolent1765
@sacredindolent1765 Жыл бұрын
Nice video! My two cents: in tip #10, I'd use unreachable!() rather than assert!(), as a small way to document why I'm asserting. Also instead of assert!(2+2==4), you can do assert_eq!(2 + 2, 4), which will give a clearer error message if it fails.
@Gisleburt
@Gisleburt 2 жыл бұрын
Nice video. For ok_or though, I noticed the error takes a String which is created with format!. Using ok_or means you'd have to allocate that memory on the heap and create that string whether you need it or not. Here you'd get a small bump from ok_or_else as this takes a function that only runs if it's needed, saving you the allocation unless the function runs. Tbf though, clippy would point this out when you run it so less necessary to address in the code review.
@strager_
@strager_ 2 жыл бұрын
You're right. In Rust today, I think .ok_or() would force the allocation. I didn't think consider that. .ok_or_else() or match is the right thing to do if efficiency is important.
@Speykious
@Speykious Жыл бұрын
​@@strager_ Clippy even warns about using ok_or_else instead in case there's an allocation. Now there's also the `let ... else` syntax which is more readable and perfectly adapted for this use-case: let Some(token) = self.values.pop() else { return Err(...); };
@Codotaku
@Codotaku Жыл бұрын
the assert isn't just mere documentation, but it also helps giving the compiler some context and guarantees so it potentially optimize further
@strager_
@strager_ 11 ай бұрын
This is a nice-to-have, but such optimizations never matter in my experience.
@leddoo
@leddoo Жыл бұрын
man, i wish the `if let &&` thing worked.. oh and about the bit ops: this seems to be a compiler, so the lower memory use could improve cache behavior.
@AdithyadevRajesh
@AdithyadevRajesh 9 ай бұрын
you could use is_some_and here instead of pattern matching, leading to `maybe_x.is_some_and(|x| x < 10)`
@DaNiel-lz2gx
@DaNiel-lz2gx 4 ай бұрын
On your final tip you .ok_or(Error { .. format!(...) } which expensively allocates a string on the heap. Instead defer it to only run a closure when needed: .ok_or_else(|| Error { .. format!(...) }
@bigtymer4862
@bigtymer4862 2 жыл бұрын
Great video, and love your rust tips. I agree with everything you had to say. Would love to see more of these!
@strager_
@strager_ 2 жыл бұрын
What are you interested in specifically? Rust videos? Tip videos? Rust tip videos?
@0xedb
@0xedb 2 жыл бұрын
@@strager_ more Rust
@v0xl
@v0xl Жыл бұрын
about the let-chains thing. You can use let-else there!
@jaysistar2711
@jaysistar2711 2 жыл бұрын
Great video. It looks like many of these apply for C, C++, and Zig as well.
@marcossidoruk8033
@marcossidoruk8033 Жыл бұрын
Instead of the unreadable if let && nonsense and instead of the nesting you could just invert the conditions and put them sequentially, like this: loop { if !condition1 { break; } if !condition2 { break; } if !condition3 { break; } DO SOMETHING HERE } That is a generally useful pattern that can reduce nesting significantly and makes all your conditions much more readable. It also works in basically any language.
@strager_
@strager_ Жыл бұрын
Guard clauses are indeed a good idea! However, they won't work for destructuring (e.g. pulling out the character from self.peek()). They would work for the other conditions though.
@hirisraharjo
@hirisraharjo 2 жыл бұрын
Great video man! Would love a bit more explanation about bit operation at minute ~2.40
@spicybaguette7706
@spicybaguette7706 Жыл бұрын
1:40 I think you can use while let Some(x @ ..10) = maybe_x
@strager_
@strager_ Жыл бұрын
Clever! Probably *too* clever...
@jeffg4686
@jeffg4686 2 жыл бұрын
just adding a note on the Copy one - for const, it doesn't matter as it just copies the value around at compile time (inlines the value) - though this is Rust, and I may be overlooking something ...
@strager_
@strager_ 2 жыл бұрын
The function copies the data to the stack in order to return Some(ADDITION_ASSIGNMENT_TOKEN) (~5 or 6 machine words). My suggestion is to instead copy a reference (1 machine word), which can be done more cheaply. The copy might disappear if the function is inlined, but I doubt a big function like this would be inlined.
@jeffg4686
@jeffg4686 2 жыл бұрын
@@strager_ - oh gotcha, makes sense
@EndermanAPM
@EndermanAPM Жыл бұрын
I understand that you might not know an answer while streaming and just roll with it. What I'm not sure is why would you keep the tip if it's invalid. (talking about the if ... let)
@strager_
@strager_ Жыл бұрын
> why would you keep the tip if it's invalid. (talking about the if ... let) It might be valid in a future version of Rust: github.com/rust-lang/rfcs/blob/master/text/2497-if-let-chains.md The takeaway is that Rust evolves and that you should look to see if there's a nicer way to write your code. If someone really wants the feature, they can implement it in the Rust compiler! github.com/rust-lang/rust/issues/53667
@alishergaliev7714
@alishergaliev7714 2 жыл бұрын
Can you please tell me the name of the font you are using?
@strager_
@strager_ 2 жыл бұрын
In my terminal/editor, I'm using Comic Code: tosche.net/fonts/comic-code
@MaxCoplan
@MaxCoplan 2 жыл бұрын
Thank you for the vid. These tips are easier than chicks after I tell them I program in Rust
@strager_
@strager_ 2 жыл бұрын
I use Rust, btw
@najidnadri792
@najidnadri792 2 жыл бұрын
My err handling: Let some_result = myfunc(); If let Err(e) = some_result { return Err(e) } let some_result = some_result.unwrap(); Is this a good way in handling error? because IMHO this is much cleaner than spamming match patterns
@optiphonic_
@optiphonic_ 2 жыл бұрын
Should just be able to go let some_result = myfunc()?;, and the Err will be automatically returned
@hamzadlm6625
@hamzadlm6625 Жыл бұрын
Blasingly fast!
@superspartanman4480
@superspartanman4480 Жыл бұрын
These videos are extremely informative and well made, thank you!
@michelvandermeiren8661
@michelvandermeiren8661 Жыл бұрын
C++ was already too complex for me but RUST is completely out of my world, only aliens can code with RUST
@taladar3itch560
@taladar3itch560 Жыл бұрын
We need a Rust++.
@martiananomaly
@martiananomaly Жыл бұрын
Imo C is way harder than both Rust and C++. It's a completely different ball game with loads of segmentation faults and ease to make memory bugs.
@laxsjo.
@laxsjo. Жыл бұрын
​@@martiananomalyI like to compare c and rust in this way. In c creating a program takes a long time, since writing the program is relatively quick, but you'll be debuging endless segfaults. Rust takes a long time because writing a program that the compiler accepts will take you a long time, but once it compiles, it will have virtually no memory bugs. You can definitely argue about which method is faster over all, and I personally prefer rusts way, but I'm not here to argue about that.
@martiananomaly
@martiananomaly Жыл бұрын
@@laxsjo. yeah, having worked a fair share with both, Rust is definitely the better language.
@max_ishere
@max_ishere 2 жыл бұрын
There is a matches!() macro for if let
going fast is about doing less
19:41
leddoo
Рет қаралды 174 М.
Big O myths busted! (Time complexity is complicated)
21:33
strager
Рет қаралды 136 М.
ТВОИ РОДИТЕЛИ И ЧЕЛОВЕК ПАУК 😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 6 МЛН
8 deadly mistakes beginner Rust developers make
14:14
Let's Get Rusty
Рет қаралды 173 М.
rust runs on EVERYTHING (no operating system, just Rust)
18:10
Low Level
Рет қаралды 362 М.
I spent six months rewriting everything in Rust
15:11
chris biscardi
Рет қаралды 441 М.
P99 CONF - Zig vs Rust
55:01
ThePrimeTime
Рет қаралды 80 М.
Optimizing with "Bad Code"
17:11
Kaze Emanuar
Рет қаралды 219 М.
What Makes Rust Different?
12:38
No Boilerplate
Рет қаралды 205 М.
Rust multi-threading code review
12:13
Tantan
Рет қаралды 198 М.
How to contribute to open source
14:15
strager
Рет қаралды 104 М.
Tips for C++ newbies (compiler code review)
5:35
strager
Рет қаралды 11 М.
Miguel Raz Guzmán Macedo - Portable SIMD tricks for fun and profit
9:53
Scientific Computing in Rust
Рет қаралды 2,9 М.