4

Rust Book Club #5: Structs!

 2 years ago
source link: https://www.youtube.com/watch?v=mBi_FsPKd9w
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
0:00 / 20:18

Rust Book Club #5: Structs!

331 views
Nov 5, 2021

A Quarter of the way through the series! Woo Hoo!

Watch someone stumble through a technical book without the polish you expect. The perfect exercise of schadenfreude and/or masochism.

If you want to learn rust, but don't have friends to talk about it with along the way, hopefully this helps you and you can feel more confident in your learning process.

Structs are cool! It's starting to feel like Objects are just Structs with extra steps, which makes me feel more confident in how I will use them for future projects 0:00 - Intro 0:41 - Structs vs Tuples 2:19 - Rust creators being nice to me 3:24 - My Previous Biases exposed 4:42 - Why Tuple Structs are needed 5:08 - Unit Structs in chapter ten 5:32 - Lifetime introduction iN cHapTeR tEn 6:38 - The Meat and Potatoes of Structs 7:13 - Me getting confused about design choices 9:19 - Methods! 10:33 - OH BOY CHAPTER TEN THE THINGS I WILL LEARN 11:37 - Rust making method calls easy 12:07 - TERMINAL TIME 12:40 - Syntax I struggled with 16:35 - How Impl's inform us about Rust's Design 19:49 - Outro

_ Source Material _ The Rust Programming Language by Steve Klabnik and Carol Nichols

______ Links ______ Raymond Hettinger's Great talk that you should watch even if you don't care about python: https://www.youtube.com/watch?v=wf-Bq...

The Rust book online: https://doc.rust-lang.org/stable/book... A physical print of the Rust book from No-Starch Press: https://nostarch.com/Rust2018

Show lessShow more

14 Comments

Sort by
default-user=s48-c-k-c0x00ffffff-no-rj
Add a public comment...

The reason for how #[derive(Debug)] works is consistency and clarity.

There are very few traits that are automatically implemented but it would not make sense for Debug. Keeping them minimal is important for language simplicity.

Imagine you make a struct where all of its fields are Debug. You print it out somewhere else. Then change a field to be non-Debug. Which means you can't print it anymore. It is clearer and easier to know if a certain struct is Debug or not this way. It also means that the way it works is exactly like other traits and also deriving other traits. Consistency makes learning it and using it easier, less special cases.

Read more 11 days ago (edited)

Best video by far in terms of snippets, closeups, code and font size. Also, I know rust reasonably well (I'm a c programmer predominantly). But your take always gives a slightly new spin and is different from anything else re this subject. You deserve more subs. That will come.... Wayne's world.... looking forward to enums... They are awesome!

11 days ago

17:30 -ish: I think a big reason APIs generally prefer to do their mutations through mutable references rather than through passing ownership is probably related to performance.

Say, for example, you have some struct `HugeStruct`, that has like a bajillion different fields, none of which are Copy. Say you have a method `change_single_part` that just goes into one of those fields and tweaks it a little bit. If you take ownership with a regular old `self` and spit out a new `HugeStruct` with the different value, you're doing a bunch of moves. The problem is that when Rust sees you move a value, what's really happening in the code the compiler generates for LLVM (as far as I can tell by a quick Google; I'm no expert) is that it will `llvm.memcpy` the old value into the new place. This means Rust's "moves" are secretly really done by 1. copying, 2. making using the original illegal afterward. Often this doesn't come up, because the compiler is often smart enough to elide `memcpy` calls if it sees that it can. But sometimes this elision fails.

If you use `&mut self` instead, then you don't have to rely on the compiler doing these optimizations for you---you can just mutate whatever's inside the struct in-place.

EDIT: another, probably bigger, reason is that if the function's caller doesn't have ownership, but they have a mutable reference, they can call `&mut self` methods, but they can't call just straight up `self` methods.

If I don't own a vector, but I do have a mutable reference to it, I'd still like to be able to call methods on that vector that mutate it, like `sort()`---whoever gave me the mutable reference was fine with me changing the vector. But if `sort()` took `self`, this basically means "if you call me, you'd better be the final arbiter of when this thing dies", which is more demanding than "you'd better have permission to mutate it" like `&mut self` would imply.

Read more 5 days ago (edited)

You can turn on argument name hints in loads of IDEs including vims and such. It will essentially look like how you imagined Something::new(this: u32, that: f64) and usually also hints at types like this let x: f64 = 0.0; the ":f64" would be put in by the editor.

11 days ago


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK