You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-16 14:17:34 +02:00
18
STYLE.md
18
STYLE.md
@ -65,6 +65,24 @@ collapsed or removed entirely from the slide.
|
|||||||
course, instructors will only have time to glance at the notes so it is not
|
course, instructors will only have time to glance at the notes so it is not
|
||||||
useful to include full paragraphs which the instructor should read out loud.
|
useful to include full paragraphs which the instructor should read out loud.
|
||||||
|
|
||||||
|
### More to Explore
|
||||||
|
|
||||||
|
Move extended explanations in the notes to a "More to Explore" section:
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
<details>
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
## More to Explore
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
</details>
|
||||||
|
```
|
||||||
|
|
||||||
|
The material there is outside the scope of the regular class.
|
||||||
|
|
||||||
## Translations
|
## Translations
|
||||||
|
|
||||||
This section is about what you write in the translation. We describe
|
This section is about what you write in the translation. We describe
|
||||||
|
@ -58,6 +58,10 @@ Key Points:
|
|||||||
* `dbg_size!(&i32)`: size 8 bytes, align: 8 bytes (on a 64-bit machine),
|
* `dbg_size!(&i32)`: size 8 bytes, align: 8 bytes (on a 64-bit machine),
|
||||||
* `dbg_size!(Option<&i32>)`: size 8 bytes, align: 8 bytes (null pointer optimization, see below).
|
* `dbg_size!(Option<&i32>)`: size 8 bytes, align: 8 bytes (null pointer optimization, see below).
|
||||||
|
|
||||||
|
## More to Explore
|
||||||
|
|
||||||
|
Rust has several optimizations it can employ to make enums take up less space.
|
||||||
|
|
||||||
* Niche optimization: Rust will merge unused bit patterns for the enum
|
* Niche optimization: Rust will merge unused bit patterns for the enum
|
||||||
discriminant.
|
discriminant.
|
||||||
|
|
||||||
|
@ -29,22 +29,25 @@ fn main() {
|
|||||||
|
|
||||||
* If students ask about it, you can mention that the underlying memory is heap allocated using the [System Allocator] and custom allocators can be implemented using the [Allocator API]
|
* If students ask about it, you can mention that the underlying memory is heap allocated using the [System Allocator] and custom allocators can be implemented using the [Allocator API]
|
||||||
|
|
||||||
* We can inspect the memory layout with `unsafe` code. However, you should point out that this is rightfully unsafe!
|
## More to Explore
|
||||||
|
|
||||||
```rust,editable
|
We can inspect the memory layout with `unsafe` Rust. However, you should point
|
||||||
fn main() {
|
out that this is rightfully unsafe!
|
||||||
let mut s1 = String::from("Hello");
|
|
||||||
s1.push(' ');
|
```rust,editable
|
||||||
s1.push_str("world");
|
fn main() {
|
||||||
// DON'T DO THIS AT HOME! For educational purposes only.
|
let mut s1 = String::from("Hello");
|
||||||
// String provides no guarantees about its layout, so this could lead to
|
s1.push(' ');
|
||||||
// undefined behavior.
|
s1.push_str("world");
|
||||||
unsafe {
|
// DON'T DO THIS AT HOME! For educational purposes only.
|
||||||
let (ptr, capacity, len): (usize, usize, usize) = std::mem::transmute(s1);
|
// String provides no guarantees about its layout, so this could lead to
|
||||||
println!("ptr = {ptr:#x}, len = {len}, capacity = {capacity}");
|
// undefined behavior.
|
||||||
}
|
unsafe {
|
||||||
|
let (ptr, capacity, len): (usize, usize, usize) = std::mem::transmute(s1);
|
||||||
|
println!("ptr = {ptr:#x}, len = {len}, capacity = {capacity}");
|
||||||
}
|
}
|
||||||
```
|
}
|
||||||
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user