From 5aec311b0952301400937a67a04495a97d80558f Mon Sep 17 00:00:00 2001 From: Boris Egorov Date: Wed, 14 Dec 2016 09:51:48 +0700 Subject: [PATCH 01/14] Revert "Travis isn't seeing our dictionary; try not copying to tmp" This reverts commit 972f916e351916e2fbb7cf7aab13526a20d1312c. --- spellcheck.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spellcheck.sh b/spellcheck.sh index f6faef6b7..bba3decc2 100755 --- a/spellcheck.sh +++ b/spellcheck.sh @@ -36,9 +36,9 @@ dict_filename=dictionary.txt markdown_sources=(./src/*.md) mode="check" -# aspell repeatedly modifies personal dictionary when checking interactively, +# aspell repeatedly modifies personal dictionary for some purpose, # so we should use a copy of our dictionary -dict_path="/tmp/dictionary.txt" +dict_path="/tmp/$dict_filename" if [[ "$1" == "list" ]]; then mode="list" @@ -55,11 +55,11 @@ if [[ ! -f "$dict_filename" ]]; then cat "${markdown_sources[@]}" | aspell --ignore 3 list | sort -u >> "$dict_filename" elif [[ "$mode" == "list" ]]; then # List (default) mode: scan all files, report errors + cp "$dict_filename" "$dict_path" declare -i retval=0 for fname in "${markdown_sources[@]}"; do - echo "command = aspell --home-dir=. --ignore 3 --personal=\"$dict_filename\" \"$mode\" < \"$fname\"" - command=$(aspell --home-dir=. --ignore 3 --personal="$dict_filename" "$mode" < "$fname") + command=$(aspell --ignore 3 --personal="$dict_path" "$mode" < "$fname") if [[ -n "$command" ]]; then for error in $command; do # TODO: Find more correct way to get line number From cf8d29add75fe4fdd8a834cf069b20095e461fe5 Mon Sep 17 00:00:00 2001 From: Boris Egorov Date: Wed, 14 Dec 2016 09:53:01 +0700 Subject: [PATCH 02/14] Revert "Try telling aspell that the current dir is its home dir too" This reverts commit 4b75fda8be45e34143a4427cc9842e30cdc6afeb. --- spellcheck.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spellcheck.sh b/spellcheck.sh index bba3decc2..4deaecc00 100755 --- a/spellcheck.sh +++ b/spellcheck.sh @@ -32,7 +32,7 @@ aspell --version shopt -s nullglob -dict_filename=dictionary.txt +dict_filename=./dictionary.txt markdown_sources=(./src/*.md) mode="check" From 010cdb0d672311095dcf5abaee4337112ceabf4f Mon Sep 17 00:00:00 2001 From: Boris Egorov Date: Wed, 14 Dec 2016 09:55:37 +0700 Subject: [PATCH 03/14] dictionary: Add a few words aspell 0.60.7 considers new spellings --- dictionary.txt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/dictionary.txt b/dictionary.txt index 8e96947cd..070c19fe4 100644 --- a/dictionary.txt +++ b/dictionary.txt @@ -34,6 +34,7 @@ chYY config Config const +constant's copyeditor couldn cratesio @@ -62,6 +63,7 @@ doccratesio doesn Edsger else's +emoji encodings enum Enum @@ -78,6 +80,7 @@ Filename filesystem Filesystem formatter +GitHub gitignore grapheme Grapheme @@ -96,9 +99,12 @@ Hoare Hola homogenous html +IEEE impl +indices init instantiation +internet InvalidDigit ioerror iokind @@ -109,6 +115,7 @@ IpAddrKind irst isize iter +JavaScript lang latin libc @@ -139,6 +146,7 @@ NotFound null's OCaml offsetof +online OptionalFloatingPointNumber OptionalNumber OsStr @@ -162,6 +170,9 @@ randcrate READMEs rect redeclaring +Refactoring +refactor +refactoring RefCell repr runtime @@ -197,22 +208,29 @@ Submodules suboptimal subtree That'd +test's TODO toml TOML tradeoff TrafficLight trpl +tuple +tuples typeof UFCS unary Unary +Uninstalling +uninstall unoptimized unsized unsynchronized +username USERPROFILE usize UsState +variable's Versioning wasn whitespace From 1ce13eceee5dbf5c4dd48e7ceacbd75ba18d5e63 Mon Sep 17 00:00:00 2001 From: Boris Egorov Date: Wed, 14 Dec 2016 10:05:52 +0700 Subject: [PATCH 04/14] Revert "Uggghhh take out spell checking from travis for now" This reverts commit ad2fc2bcf07828c0112b7750695b6d2ee9adcf8b. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 15ca41a17..f8b6a5f42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,7 @@ addons: before_script: - (cargo install mdbook --git https://github.com/azerupi/mdBook.git --force || true) script: + - bash spellcheck.sh list - PATH=$PATH:/home/travis/.cargo/bin mdbook test - PATH=$PATH:/home/travis/.cargo/bin mdbook build - cargo run --bin lfp src From 4b82f8a99d413246a9569bce05240caf47a7de0e Mon Sep 17 00:00:00 2001 From: Boris Egorov Date: Wed, 14 Dec 2016 10:25:11 +0700 Subject: [PATCH 05/14] spellcheck: Exit with error on missing dictionary file --- spellcheck.sh | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/spellcheck.sh b/spellcheck.sh index 4deaecc00..224e4a9a9 100755 --- a/spellcheck.sh +++ b/spellcheck.sh @@ -55,9 +55,15 @@ if [[ ! -f "$dict_filename" ]]; then cat "${markdown_sources[@]}" | aspell --ignore 3 list | sort -u >> "$dict_filename" elif [[ "$mode" == "list" ]]; then # List (default) mode: scan all files, report errors - cp "$dict_filename" "$dict_path" declare -i retval=0 + cp "$dict_filename" "$dict_path" + + if [ ! -f $dict_path ]; then + retval=1 + exit "$retval" + fi + for fname in "${markdown_sources[@]}"; do command=$(aspell --ignore 3 --personal="$dict_path" "$mode" < "$fname") if [[ -n "$command" ]]; then @@ -74,6 +80,12 @@ elif [[ "$mode" == "list" ]]; then elif [[ "$mode" == "check" ]]; then # Interactive mode: fix typos cp "$dict_filename" "$dict_path" + + if [ ! -f $dict_path ]; then + retval=1 + exit "$retval" + fi + for fname in "${markdown_sources[@]}"; do aspell --ignore 3 --dont-backup --personal="$dict_path" "$mode" "$fname" done From f828919e62aa542aaaae03c1fb565da42374213e Mon Sep 17 00:00:00 2001 From: Manny Jois Date: Sat, 17 Dec 2016 12:32:02 -0800 Subject: [PATCH 06/14] Added typical disclaimer about curl | sh. --- src/ch01-01-installation.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ch01-01-installation.md b/src/ch01-01-installation.md index e30b4e9ee..da0b0391e 100644 --- a/src/ch01-01-installation.md +++ b/src/ch01-01-installation.md @@ -27,6 +27,9 @@ your password. If it all goes well, you’ll see this appear: Rust is installed now. Great! ``` +Of course, if you disapprove of the `curl | sh` pattern, you can download, inspect +and run the script however you like. + ### Installing on Windows On Windows, go to [https://rustup.rs](https://rustup.rs/) and From 8a987a28e2e7da748726791c2c385ea930c8790a Mon Sep 17 00:00:00 2001 From: aaaxx Date: Sat, 17 Dec 2016 23:10:18 +0100 Subject: [PATCH 07/14] Fix typo --- spellcheck.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spellcheck.sh b/spellcheck.sh index 224e4a9a9..0a2d16852 100755 --- a/spellcheck.sh +++ b/spellcheck.sh @@ -23,7 +23,7 @@ aspell --version # 1 if any errors found, # 0 if all is clear. -# Script skips words with length less then or equal to 3. This helps to avoid +# Script skips words with length less than or equal to 3. This helps to avoid # some false positives. # We can consider skipping source code in markdown files (```code```) to reduce From 42c3fbad3d5b0797c1c576989063f22570cc69d1 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 18 Dec 2016 14:34:22 +0100 Subject: [PATCH 08/14] Fix off-by-one mistake in while loop explanation --- src/ch03-05-control-flow.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ch03-05-control-flow.md b/src/ch03-05-control-flow.md index e813bf4d4..f2e1d3584 100644 --- a/src/ch03-05-control-flow.md +++ b/src/ch03-05-control-flow.md @@ -383,7 +383,7 @@ the value is: 50 ``` All five array values appear in the terminal, as expected. Even though `index` -will reach a value of `6` at some point, the loop stops executing before trying +will reach a value of `5` at some point, the loop stops executing before trying to fetch a sixth value from the array. But this approach is error prone; we could cause the program to panic if the From 5e5919fe83c10ec3cc30b60555939ba99d95c1fe Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Thu, 22 Dec 2016 18:46:44 -0500 Subject: [PATCH 09/14] Only build the master branch; we usually have PRs for other branches Now that lots of other rust-lang stuff is using travis, I don't want to take 2 spots in the queue every time we push to a PR! --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index f8b6a5f42..b26b252dc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,9 @@ rust: - nightly - beta - stable +branches: + only: + - master addons: apt: packages: From 20c8df08ea7314a904633be11ef5fb4f818d68af Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 26 Dec 2016 15:56:03 -0500 Subject: [PATCH 10/14] Move nightly to appendix --- src/SUMMARY.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 06a3ab8dd..d37fa6463 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -119,11 +119,8 @@ - [Macros]() - [Writing Your Own Macros]() -- [Nightly Rust]() (?) - - [Nightly Features]() - - [How to Find Out About Nightly Features]() - - [Appendix](appendix-00.md) - [Keywords](appendix-01-keywords.md) - [Operators](appendix-02-operators.md) - [Derivable Traits](appendix-03-derivable-traits.md) + - [Nightly Rust](appendix-04-nightly-rust.md) From 8919f24fc1b280a1fbd53d4926dbace70e6f0482 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 26 Dec 2016 15:56:56 -0500 Subject: [PATCH 11/14] Remove FFI - let's do a separate minibook instead. --- src/SUMMARY.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index d37fa6463..2b3965d02 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -104,12 +104,6 @@ - [Raw Pointers]() - [`transmute`]() -- [Foreign Function Interface]() (?) - - [Conditional Compilation]() - - [Bindings to C]() - - [Using Rust from Other Languages]() - - [`static`]() - - [Advanced Type System Features]() (perhaps called "Advanced Traits"?) - [Associated Types]() - [Trait Objects]() (might be incorporated into the Composition chapter?) From 3a0ff3c7c187b25fd0418ecfd821da3f944326b2 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 26 Dec 2016 16:03:45 -0500 Subject: [PATCH 12/14] More notes, small changes to TOC --- src/SUMMARY.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 2b3965d02..152507670 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -64,7 +64,7 @@ ## Thinking in Rust -- [Composition]() +- [Thinking in Rust]() - [Instead of Inheritance]() - [Trait Objects?]() @@ -100,17 +100,16 @@ - [More Lifetimes]() -- [Unsafe Rust]() +- [Unsafe Rust]() (think about merging into the next chapter on advanced type system features) - [Raw Pointers]() - [`transmute`]() - [Advanced Type System Features]() (perhaps called "Advanced Traits"?) - [Associated Types]() - - [Trait Objects]() (might be incorporated into the Composition chapter?) - [UFCS]() - [Coherence]() -- [Macros]() +- [Macros]() (think about making little book of rust macros the authority on this topic) - [Writing Your Own Macros]() - [Appendix](appendix-00.md) From 948b1b417767f127e0062282b8534f51f61227ba Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 26 Dec 2016 16:33:28 -0500 Subject: [PATCH 13/14] Outline for cargo chapter --- src/ch14-00-more-about-cargo.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/ch14-00-more-about-cargo.md diff --git a/src/ch14-00-more-about-cargo.md b/src/ch14-00-more-about-cargo.md new file mode 100644 index 000000000..7440afd4c --- /dev/null +++ b/src/ch14-00-more-about-cargo.md @@ -0,0 +1,21 @@ +# More about Cargo and Crates.io + +## Getting your project ready to be published on Crates.io + +(and publish itself) + +### Metadata in your Cargo.toml + +### Versions + +- yank + +### `pub use` + +### Doc Comments + +## Release profiles + +## Cargo Workspaces + +## `cargo install` and Cargo Extensions \ No newline at end of file From 817d64276b063d5b1eca0a3fb6521ead0b108001 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 26 Dec 2016 19:35:13 -0500 Subject: [PATCH 14/14] Really rough outline notes --- src/SUMMARY.md | 50 ++------ src/appendix-04-nightly-rust.md | 1 + src/ch13-00-functional-features.md | 39 ++++++ src/ch15-00-smart-pointers.md | 112 ++++++++++++++++++ ...-concurrency.md => ch16-00-concurrency.md} | 100 ++++++++++++++-- src/ch17-00-oop.md | 73 ++++++++++++ src/{chXX-patterns.md => ch18-00-patterns.md} | 0 src/ch19-00-more-lifetimes.md | 11 ++ src/ch20-00-advanced-types.md | 56 +++++++++ 9 files changed, 393 insertions(+), 49 deletions(-) create mode 100644 src/appendix-04-nightly-rust.md create mode 100644 src/ch13-00-functional-features.md create mode 100644 src/ch15-00-smart-pointers.md rename src/{ch18-00-concurrency.md => ch16-00-concurrency.md} (64%) create mode 100644 src/ch17-00-oop.md rename src/{chXX-patterns.md => ch18-00-patterns.md} (100%) create mode 100644 src/ch19-00-more-lifetimes.md create mode 100644 src/ch20-00-advanced-types.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 152507670..10a378234 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -47,7 +47,7 @@ - [Generics](ch10-00-generics.md) - [Syntax](ch10-01-syntax.md) - - [Traits](ch10-02-traits.md) + - [Traits](ch10-02-traits.md) - NEED DEFAULT METHOD IMPLEMENTATIONS - [Lifetime syntax](ch10-03-lifetime-syntax.md) - [Testing](ch11-00-testing.md) @@ -56,58 +56,26 @@ - [Test Organization](ch11-03-test-organization.md) - [I/O]() - - [`Read` & `Write`]() - - [`std::fs`]() - - [`std::path`]() - - [`std::env`]() - ## Thinking in Rust -- [Thinking in Rust]() - - [Instead of Inheritance]() - - [Trait Objects?]() +- [Functional Language Features in Rust - Iterators and Closures](ch13-00-functional-features.md) -- [Creating a Library]() - - [Cargo]() - - [Crates.io]() - - [Organizing your Public API](chYY-YY-public-api.md) - - [Documentation](chYY-YY-documentation.md) - - [Workspaces and Multiple Related Crates](chYY-YY-workspaces.md) +- [More about Cargo and Crates.io](ch14-00-more-about-cargo.md) -- [Closures]() +- [Smart Pointers](ch15-00-smart-pointers.md) -- [Zero-cost Abstractions]() - - [Iterators as a Case Study]() +- [Concurrency](ch16-00-concurrency.md) -- [Smart Pointers]() - - [`Box`]() - - [`Rc`]() - - [`Cell`]() - - [`RefCell`]() - - [Interior Mutability]() - -- [Concurrency](ch18-00-concurrency.md) - - [Threads]() - - [`Send` & `Sync`]() - - [`Arc`]() - - [`Mutex`]() - - [`Channels`]() +- [Is Rust OOP?](ch17-00-oop.md) ## Advanced Topics -- [Patterns](chXX-patterns.md) +- [Patterns](ch18-00-patterns.md) (perhaps an appendix?) -- [More Lifetimes]() +- [More Lifetimes](ch19-00-more-lifetimes.md) (perhaps merge this into advanced type system features?) -- [Unsafe Rust]() (think about merging into the next chapter on advanced type system features) - - [Raw Pointers]() - - [`transmute`]() - -- [Advanced Type System Features]() (perhaps called "Advanced Traits"?) - - [Associated Types]() - - [UFCS]() - - [Coherence]() +- [Advanced Type System Features](ch20-00-advanced-types.md) (perhaps called "Advanced Traits"?) - [Macros]() (think about making little book of rust macros the authority on this topic) - [Writing Your Own Macros]() diff --git a/src/appendix-04-nightly-rust.md b/src/appendix-04-nightly-rust.md new file mode 100644 index 000000000..f99a3f3b8 --- /dev/null +++ b/src/appendix-04-nightly-rust.md @@ -0,0 +1 @@ +# Nightly Rust diff --git a/src/ch13-00-functional-features.md b/src/ch13-00-functional-features.md new file mode 100644 index 000000000..9269638db --- /dev/null +++ b/src/ch13-00-functional-features.md @@ -0,0 +1,39 @@ +# Functional Language features in Rust - Iterators and Closures + +## Closures + +### What is a closure + +How are they diff from fns + +### `Fn` traits + +## Iterators + +### Iterator & for loop + +.into_iter() + +### Iterators are Lazy + +Difference between adapter and consumer - another iterator or consuming? + +### Implementing the Iterator trait + +Talk about using Associated Types here, foreshadow to advanced type systems +chapter about why this is a different thing than normal + +## ??? How does this improve `greprs` + +Does this get woven into the above sections? + +## Summary: Performance + +### Iterators compile down to ASM == for loop + +Most complicated chain of iterator functions that compile down to the same ASM as a for loop + +### Representation: Closures are a Struct + +Closures don't have any further perf penalty over regular fn calls + diff --git a/src/ch15-00-smart-pointers.md b/src/ch15-00-smart-pointers.md new file mode 100644 index 000000000..5273b495c --- /dev/null +++ b/src/ch15-00-smart-pointers.md @@ -0,0 +1,112 @@ +# Smart Pointers + +By smart pointers we mean a reference with more characteristics. + +Example of something that doesn't work + +Surprise! Vec and String are technically smart pointers too! + +This chapter is not a comprehensive list, but will give some examples of the +ones in the standard library. + + +## `Box` + +Don't use very often in your own code +Heap allocated +Express Ownership of a heap allocated thing + +The three situations to use Box + +1. Trait objects +2. Recursive data structures +3. Extend the lifetime of something + +How this interacts with the Drop trait + +## `Rc` + +Reference counted. Rc is for *multiple ownership* - this thing should get +deallocated when all of the owners go out of scope. + +Show the data structure: + +```rust +struct Rc { + data: Box, + strong_reference_count: usize, + weak_reference_count: usize, +} +``` + +Talk through this. + +This only works if the data is immutable. + +What happens when you clone an Rc: data isn't cloned, increase the strong count. +When an Rc clone goes out of scope, the count goes down. + +### Rc Cycles + +This is how you leak memory in rust, which btw is totally safe. + +Is this garbage collecting? Well it's not tracing GC... if you use Rc and had +a cycle detector, it would be functionally equivalent to a tracing GC. Different +runtime characteristics tho. + + +#### Solution: turn an Rc into a `Weak` + +Same as Rc, but doesn't count towards the strong ref count. When you do this, the +strong ref count goes down and the weak count goes up. + +Data gets cleaned up when the strong count is 0, no matter what the weak count is. + +Why is the weak count needed then???? + + +## `RefCell` + +Single owner of mutable data + +The ownership rules checked at runtime instead of compile time. + +Only single threaded. See next chapter. + +### `borrow` and `borrow_mut` methods + +Checks all the rules and panics at runtime if the code violates them. + +1. The borrow checker is conservative and people can know more things. (no you +don't, but if you really want to go back to debugging segfaults, feel free) + +2. For when you're only allowed to have an immutable thing (which could be `Rc`) +but you need to be able to mutate the underlying data. + +## `Cell` + +Same thing as RefCell but for types that are Copy. No borrow checking rules here +anyway. So just reason #2 above. + +## Is this really safe? Yes srsly + +RefCell is still doing the checks, just at runtime +Cell is safe bc Copy types don't need the ownership rules anyway + +### The Interior Mutability Pattern + +The Interior Mutability Pattern is super unsafe internally but safe to use +from the outside and is totally safe, totally, trust us, seriously, it's safe. + +Allude to `UnsafeCell` maybe. Affects optimizations since &mut T is unique. +UnsafeCell turns off those optimizations so that everything doesn't break. + +This is how you can opt-out of the default of Rust's ownership rules and opt +in to different guarantees. + +## Summary + +If you want to implement your own smart pointer, go read the Nomicon. + +Now let's talk about concurrency, and some smart pointers that can be used +with multiple threads. diff --git a/src/ch18-00-concurrency.md b/src/ch16-00-concurrency.md similarity index 64% rename from src/ch18-00-concurrency.md rename to src/ch16-00-concurrency.md index 9c32ee762..9bed80f0a 100644 --- a/src/ch18-00-concurrency.md +++ b/src/ch16-00-concurrency.md @@ -1,17 +1,101 @@ -# Concurrency - -This is a really rough sketch of some ideas that this chapter might cover. - -From a comment of steveklabnik's on [the orange website]. "that paper" refers to [Boehm 2004]. - -[the orange website]: https://news.ycombinator.com/item?id=13078384 -[Boehm 2004]: http://www.hpl.hp.com/techreports/2004/HPL-2004-209.pdf +# Fearless Concurrency So, with Rust, it's more subtle than that. That is, while threading proper isn't part of the language itself, Rust's type system is structured in such a way as to make it possible to build those kinds of libraries. In other words, Rust's focus on aliasability ends up solving these problems. +This is a library abstraction. + +Shared mutable state is a problem. Both useful. Functional langs get rid of +mutability. + +Ownership rules (that tame the "shared" aspect) enable fearless concurrency: the +compiler is making sure you don't shoot yourself in your foot. + +## What are threads + + + +## Rust's concurrency tradeoffs + +Lots of different languages tackle this problem in different ways. We are not +going to talk about that: exercise for the reader is investigate other langs +and compare and contrast with Rust's approach. + +This is how Rust does it, what rust means by threads + +OS threads are exposed in the standard library bc a systems programming language +should integrate with your system. + +If you have a different threaded mechanism, you need a runtime, rust is trying +to not have a heavy runtime. + +These are the reasons Rust's concurrency model is this way as opposed to other +lang's ways, which are optimizing for different things. + + +## Let's get a thread: `thread::spawn` + +Code examples - just print stuff, no data sharing + +## Communicating between threads + +### `Channels` + +Look up examples of cases where channels are useful + +Can match modeling of certain problems + +#### `Send` + +Send is a trait that means i'm allowed to transfer ownership to another thread +down a channel + +What things can be send and what can't? + +## Sharing data between threads + +Try to share data and get an error about which trait it doesn't implement + +### `Sync` + +It's ok to access a thing from multiple threads at once + +Immutable things can be sync easily. + +### `Arc` + +Atomic Reference Counting. Inner data still has to be immutable. + +Steve knows the motivating code that goes here. + +### `Mutex` + +For mutabe data. + +`lock` method, you get a Mutex guard. Change, then unlock, which usually happens +automatically when the Mutex guard goes out of scope. If you do this wrong, your +code will hang. + +Deadlocks are safe, you have to manage that yourself. Deadlock bugs usually +happen bc you forget to unlock, but drop unlocks automatically. + + +## Maybe make `greprs` concurrent? + +Might be boilerplatey without scoped threads, maybe just allude. + + + +This is a really rough sketch of some ideas that this chapter might cover. + +From a comment of steveklabnik's on [the definitely not orange website]. "that paper" refers to [Boehm 2004]. + +[the orange website]: https://news.ycombinator.com/item?id=13078384 +[Boehm 2004]: http://www.hpl.hp.com/techreports/2004/HPL-2004-209.pdf + + So for example, in that paper, 4.1 is about the problem of concurrent modifiability. And indeed, it says diff --git a/src/ch17-00-oop.md b/src/ch17-00-oop.md new file mode 100644 index 000000000..7d0018e1f --- /dev/null +++ b/src/ch17-00-oop.md @@ -0,0 +1,73 @@ +# Is Rust OOP? + +Aphorism: DRY + +So how do you share code? + +I'm used to doing things to solve problems, what do i do instead? +Why do i need to do different things in Rust? Let's look at an example +with the Command pattern. + +## Command pattern + +Look up official def + +Want caller to be able to customize what gets done + +Method takes command object, calls a run fn + +How do we say "we want a thing that has a run function"? Answer: Traits! + +where T: Run + +This is the definition of the Fn trait! So we wouldn't implement this, we'd just +pass closures in + +## Supertraits + +Trait constraints that use other traits + +Copy requires Clone because Copy is a subset of Clone's behavior, since if you +have one, you can trivially implement the other. + +Traits that need behavior of another trait in a default method or something. + +## Trait objects + +Runtime decisions about deciding what shared code we use + +Give example code + +With traits, libraries are extendable. This is why trait objects are different +than having an enum and a match statement that has to be exhaustive at compile +time and we have to know all the things at compile time and no one can add +new things to the set of possible things + +T: trait is a compile time decision, monomorphization == static dispatch + +when you implement this trait, you get this other shared behavior + +dynamic dispatch (C++) + +### Implementation details + +- Like how other languages implement oo. + +### How to use it + +- Statically checked duck typing + +## Builder pattern + +When you don't know how many arguments you're going to have + +## Delegation + +Deref - be mad + +Deref is a way to delegate everything, if you don't want that, then write +boilerplate. Sending messages to your components. + +## How do you share data? + +Answer: get and set methods, this is awkward and might get better someday. diff --git a/src/chXX-patterns.md b/src/ch18-00-patterns.md similarity index 100% rename from src/chXX-patterns.md rename to src/ch18-00-patterns.md diff --git a/src/ch19-00-more-lifetimes.md b/src/ch19-00-more-lifetimes.md new file mode 100644 index 000000000..535e16108 --- /dev/null +++ b/src/ch19-00-more-lifetimes.md @@ -0,0 +1,11 @@ +# More Lifetimes + +## Lifetimes that depend on other lifetimes + +'a: 'b stuff: subtyping + +## Higher ranked trait bounds + +for<'a> + +Needed for closures diff --git a/src/ch20-00-advanced-types.md b/src/ch20-00-advanced-types.md new file mode 100644 index 000000000..51f4899d4 --- /dev/null +++ b/src/ch20-00-advanced-types.md @@ -0,0 +1,56 @@ +# Advanced Type System Features + +## Unsafe Rust + +Things you may do in an unsafe block that you may not in safe rust + +- deref a raw pointer +- call an unsafe fn +- access or modify a static variable +- impl an unsafe trait + +Go see other stuff + +Here's the syntax tho + +You know unsafe blocks are the cause of any crashes + +wrap all the unsafe, make it as small as possible, present a safe public API + +### Raw Pointers + +### Unsafe Functions + +#### `transmute` + +never ever. don't. stop. + +#### `extern fn` + +You have to write unsafe code to FFI + +### `static` + +### Unsafe Traits + +## Associated Types + +More common than the other things, less common than the rest of the book + +why this is a thing instead of a generic + +## The Thing Formerly Known as UFCS + +Only needed when implementing super generic code + +Lots of things are syntax sugar for this + +Two traits that impl the same method - how to disambiguate + +## Coherence + +Show examples of when you control traits and types or not + +Ex: Cannot impl Debug on someone else's type + +Solution: newtype