google / comprehensive-rust

This is the Rust course used by the Android team at Google. It provides you the material to quickly teach Rust.
https://google.github.io/comprehensive-rust/
Apache License 2.0
27.98k stars 1.67k forks source link

Add implementation-details segment #2392

Open djmitche opened 1 month ago

djmitche commented 1 month ago

This is a work-in-progress. I will probably need some help with some of these!

djmitche commented 1 month ago

I think "dynamic array layout" refers to the differences in how C++ and Rust handle empty slices?

The other item in #2384 is "Return Value Optimization". What else should we include here?

djmitche commented 1 month ago

@fw-immunant @errge @anforowicz -- I've implemented a derivative of the linked list that started #1820 as an exercise, but I'm not sure I like it.

For one thing, I was surprised to find that even with T=u64, the List enum still takes 16 bytes (8 for T, 8 for the box, with the NULL value of the box used to signify List::Nil). That's a little disappointing.

Also, this exercise ends up getting into the question of whether the head node is stored on the stack. With the List enum (top of the exercise), it is, while with the List struct ("solution"), it is not. The result is that the two arguably use the same space, and differ only in where they allocate a little bit of it.

I'm curious for your thoughts, or if you have any better ideas for an exercise related to the niche optimization. I have also added a slide on empty containers and std::ptr::NonNull::dangle(), but this segment comes before the unsafe segment so I don't think it's OK to ask students to wor with dangle().

I'd also be open to other slides to add to this segment, especially if they lead naturally to an exercise.