1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-26 09:12:58 +02:00

Indent code in speaker notes (#476)

* Indent code in speaker notes

#475

* Update stack.md

* Update destructuring-arrays.md

* Update hashmap.md

* Update traits.md
This commit is contained in:
Charisee Chiw 2023-03-11 14:12:32 -08:00 committed by GitHub
parent 368f3ba302
commit 59d3d7f625
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 121 additions and 122 deletions

View File

@ -65,19 +65,19 @@ Key Points:
types](https://doc.rust-lang.org/std/option/#representation), Rust guarantees
that `size_of::<T>()` equals `size_of::<Option<T>>()`.
Example code if you want to show how the bitwise representation *may* look like in practice.
It's important to note that the compiler provides no guarantees regarding this representation, therefore this is totally unsafe.
Example code if you want to show how the bitwise representation *may* look like in practice.
It's important to note that the compiler provides no guarantees regarding this representation, therefore this is totally unsafe.
```rust,editable
use std::mem::transmute;
```rust,editable
use std::mem::transmute;
macro_rules! dbg_bits {
macro_rules! dbg_bits {
($e:expr, $bit_type:ty) => {
println!("- {}: {:#x}", stringify!($e), transmute::<_, $bit_type>($e));
};
}
}
fn main() {
fn main() {
// TOTALLY UNSAFE. Rust provides no guarantees about the bitwise
// representation of types.
unsafe {
@ -100,25 +100,25 @@ fn main() {
dbg_bits!(None::<&i32>, usize);
dbg_bits!(Some(&0i32), usize);
}
}
```
}
```
More complex example if you want to discuss what happens when we chain more than 256 `Option`s together.
More complex example if you want to discuss what happens when we chain more than 256 `Option`s together.
```rust,editable
#![recursion_limit = "1000"]
```rust,editable
#![recursion_limit = "1000"]
use std::mem::transmute;
use std::mem::transmute;
macro_rules! dbg_bits {
macro_rules! dbg_bits {
($e:expr, $bit_type:ty) => {
println!("- {}: {:#x}", stringify!($e), transmute::<_, $bit_type>($e));
};
}
}
// Macro to wrap a value in 2^n Some() where n is the number of "@" signs.
// Increasing the recursion limit is required to evaluate this macro.
macro_rules! many_options {
// Macro to wrap a value in 2^n Some() where n is the number of "@" signs.
// Increasing the recursion limit is required to evaluate this macro.
macro_rules! many_options {
($value:expr) => { Some($value) };
($value:expr, @) => {
Some(Some($value))
@ -126,9 +126,9 @@ macro_rules! many_options {
($value:expr, @ $($more:tt)+) => {
many_options!(many_options!($value, $($more)+), $($more)+)
};
}
}
fn main() {
fn main() {
// TOTALLY UNSAFE. Rust provides no guarantees about the bitwise
// representation of types.
unsafe {
@ -149,7 +149,7 @@ fn main() {
dbg_bits!(many_options!(Some(true), @@@@@@@@), u16);
dbg_bits!(many_options!(None::<bool>, @@@@@@@@), u16);
}
}
```
}
```
</details>

View File

@ -31,8 +31,8 @@ fn main() {
* We can inspect the memory layout with `unsafe` code. However, you should point out that this is rightfully unsafe!
```rust,editable
fn main() {
```rust,editable
fn main() {
let mut s1 = String::from("Hello");
s1.push(' ');
s1.push_str("world");
@ -43,8 +43,8 @@ fn main() {
let (capacity, ptr, len): (usize, usize, usize) = std::mem::transmute(s1);
println!("ptr = {ptr:#x}, len = {len}, capacity = {capacity}");
}
}
```
}
```
</details>

View File

@ -12,22 +12,22 @@ You can destructure arrays, tuples, and slices by matching on their elements:
* Destructuring of slices of unknown length also works with patterns of fixed length.
```rust,editable
fn main() {
```rust,editable
fn main() {
inspect(&[0, -2, 3]);
inspect(&[0, -2, 3, 4]);
}
}
#[rustfmt::skip]
fn inspect(slice: &[i32]) {
#[rustfmt::skip]
fn inspect(slice: &[i32]) {
println!("Tell me about {slice:?}");
match slice {
&[0, y, z] => println!("First is 0, y = {y}, and z = {z}"),
&[1, ..] => println!("First is 1 and the rest were ignored"),
_ => println!("All elements were ignored"),
}
}
```
}
```
* Create a new pattern using `_` to represent an element.
* Add more values to the array.

View File

@ -42,13 +42,12 @@ fn main() {
* Types that implement a given trait may be of different sizes. This makes it impossible to have things like `Vec<Greet>` in the example above.
* `dyn Greet` is a way to tell the compiler about a dynamically sized type that implements `Greet`.
* In the example, `pets` holds Fat Pointers to objects that implement `Greet`. The Fat Pointer consists of two components, a pointer to the actual object and a pointer to the virtual method table for the `Greet` implementation of that particular object.
Compare these outputs in the above example:
```rust,ignore
* Compare these outputs in the above example:
```rust,ignore
println!("{} {}", std::mem::size_of::<Dog>(), std::mem::size_of::<Cat>());
println!("{} {}", std::mem::size_of::<&Dog>(), std::mem::size_of::<&Cat>());
println!("{}", std::mem::size_of::<&dyn Greet>());
println!("{}", std::mem::size_of::<Box<dyn Greet>>());
```
```
</details>