1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-12-10 12:54:53 +02:00

Merge pull request #185 from markozagar/zagar

Edits to Day1 Morning material
This commit is contained in:
Andrew Walbran 2023-01-19 11:55:12 +00:00 committed by GitHub
commit eb9f873466
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 8 deletions

View File

@ -26,16 +26,21 @@ fn main() {
``` ```
<details> <details>
Key points: Key points:
Arrays: Arrays:
* Arrays have elements of the same type, `T`, and length, `N`, which is a compile-time constant. * Arrays have elements of the same type, `T`, and length, `N`, which is a compile-time constant.
Note that the length of the array is *part of its type*, which means that `[u8; 3]` and
`[u8; 4]` are considered two different types.
* We can use literals to assign values to arrays. * We can use literals to assign values to arrays.
* In the main function, the print statement asks for the debug implementation with the `?` format parameter: `{a}` gives the default output, `{a:?}` gives the debug output. * In the main function, the print statement asks for the debug implementation with the `?` format
parameter: `{}` gives the default output, `{:?}` gives the debug output. We
could also have used `{a}` and `{a:?}` without specifying the value after the
format string.
* Adding `#`, eg `{a:#?}`, invokes a "pretty printing" format, which can be easier to read. * Adding `#`, eg `{a:#?}`, invokes a "pretty printing" format, which can be easier to read.
@ -45,6 +50,11 @@ Tuples:
* Tuples group together values of different types into a compound type. * Tuples group together values of different types into a compound type.
* Fields that can be accessed by the period and the index of the value, e.g. `t.0`, `t.1`. * Fields of a tuple can be accessed by the period and the index of the value, e.g. `t.0`, `t.1`.
* The empty tuple `()` is also known as the "unit type". It is both a type, and
the only valid value of that type - that is to say both the type and its value
are expressed as `()`. It is used to indicate, for example, that a function or
expression has no return value, as we'll see in a future slide.
</details> </details>

View File

@ -11,9 +11,18 @@ fn main() {
} }
``` ```
Some differences from C++: Some notes:
* We must dereference `ref_x` when assigning to it, similar to C pointers, * We must dereference `ref_x` when assigning to it, similar to C and C++ pointers.
* Rust will auto-dereference in some cases, in particular when invoking * Rust will auto-dereference in some cases, in particular when invoking
methods (try `count_ones`). methods (try `ref_x.count_ones()`).
* References that are declared as `mut` can be bound to different values over their lifetime. * References that are declared as `mut` can be bound to different values over their lifetime.
<details>
Key points:
* Be sure to note the difference between `let mut ref_x: &i32` and `let ref_x:
&mut i32`. The first one represents a mutable reference which can be bound to
different values, while the second represents a reference to a mutable value.
</details>

View File

@ -7,6 +7,7 @@ Rust is built with all the experience gained in the last 40 years.
* Enums and pattern matching. * Enums and pattern matching.
* Generics. * Generics.
* No overhead FFI. * No overhead FFI.
* Zero-cost abstractions.
## Tooling ## Tooling
@ -18,6 +19,15 @@ Rust is built with all the experience gained in the last 40 years.
Key points: Key points:
* Zero-cost abstractions, similar to C++, means that you don't have to 'pay'
for higher-level programming constructs with memory or CPU. For example,
writing a loop using `for` should result in roughly the same low level
instructions as using the `.iter().fold()` construct.
* It may be worth mentioning that Rust enums are 'Algebraic Data Types', also
known as 'sum types', which allow the type system to express things like
`Option<T>` and `Result<T, E>`.
* Remind people to read the errors --- many developers have gotten used to * Remind people to read the errors --- many developers have gotten used to
ignore lengthly compiler output. The Rust compiler is significantly more ignore lengthly compiler output. The Rust compiler is significantly more
talkative than other compilers. It will often provide you with _actionable_ talkative than other compilers. It will often provide you with _actionable_