mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-02-09 12:13:52 +02:00
Day2: Afternoon - speaker note details and minor cosmetic changes. (#408)
* A few speaker notes in Day2: Afternoon and minor cosmetic changes. * Do not test filesystem example code block.
This commit is contained in:
parent
93d61b2e7d
commit
fe21b773e7
@ -27,6 +27,6 @@ fn main() {
|
||||
|
||||
* Packages provide functionality and include a `Cargo.toml` file that describes how to build a bundle of 1+ crates.
|
||||
* Crates are a tree of modules, where a binary crate creates an executable and a library crate compiles to a library.
|
||||
* Modules define organization, scope, and is the focus of this section.
|
||||
* Modules define organization, scope, and are the focus of this section.
|
||||
|
||||
</details>
|
||||
|
@ -20,3 +20,34 @@ The `crate` root is in:
|
||||
|
||||
* `src/lib.rs` (for a library crate)
|
||||
* `src/main.rs` (for a binary crate)
|
||||
|
||||
<details>
|
||||
|
||||
* The change from `module/mod.rs` to `module.rs` doesn't preclude the use of submodules in Rust 2018.
|
||||
(It was mandatory in Rust 2015.)
|
||||
|
||||
The following is valid:
|
||||
|
||||
```ignore
|
||||
src/
|
||||
├── main.rs
|
||||
├── top_module.rs
|
||||
└── top_module/
|
||||
└── sub_module.rs
|
||||
```
|
||||
|
||||
* The main reason for the change is to prevent many files named `mod.rs`, which can be hard
|
||||
to distinguish in IDEs.
|
||||
|
||||
* Rust will look for modules in `modulename/mod.rs` and `modulename.rs`, but this can be changed
|
||||
with a compiler directive:
|
||||
|
||||
```rust,ignore
|
||||
#[path = "some/path.rs"]
|
||||
mod some_module { }
|
||||
```
|
||||
|
||||
This is useful, for example, if you would like to place tests for a module in a file named
|
||||
`some_module_test.rs`, similar to the convention in Go.
|
||||
|
||||
</details>
|
||||
|
@ -47,5 +47,20 @@ fn main() {
|
||||
.or_insert(374);
|
||||
```
|
||||
* Unlike `vec!`, there is unfortunately no standard `hashmap!` macro.
|
||||
* Although, since Rust 1.56, HashMap implements [`From<[(K, V); N]>`][1], which allows us to easily initialize a hash map from a literal array:
|
||||
|
||||
```rust,ignore
|
||||
let page_counts = HashMap::from([
|
||||
("Harry Potter and the Sorcerer's Stone".to_string(), 336),
|
||||
("The Hunger Games".to_string(), 374),
|
||||
]);
|
||||
```
|
||||
|
||||
* Alternatively HashMap can be built from any `Iterator` which yields key-value tuples.
|
||||
* We are showing `HashMap<String, i32>`, and avoid using `&str` as key to make examples easier. Using references in collections can, of course, be done,
|
||||
but it can lead into complications with the borrow checker.
|
||||
* Try removing `to_string()` from the example above and see if it still compiles. Where do you think we might run into issues?
|
||||
|
||||
[1]: https://doc.rust-lang.org/std/collections/hash_map/struct.HashMap.html#impl-From%3C%5B(K%2C%20V)%3B%20N%5D%3E-for-HashMap%3CK%2C%20V%2C%20RandomState%3E
|
||||
|
||||
</details>
|
||||
|
@ -27,8 +27,7 @@ fn main() {
|
||||
|
||||
<details>
|
||||
|
||||
* `len` returns the size of the `String` in bytes, not its length in characters.
|
||||
* `chars` returns an iterator over the actual characters.
|
||||
* `String` implements `Deref<Target = str>` which transparently gives it access to `str`'s methods.
|
||||
* `len()` returns the size of the `String` in bytes, not its length in characters.
|
||||
* `chars()` returns an iterator over the actual characters.
|
||||
|
||||
</details>
|
||||
|
Loading…
x
Reference in New Issue
Block a user