mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 21:31:17 -06:00
Merge pull request #472 from rust-lang/ch19-advanced-features.md
merge chapter 19-20 into 19
This commit is contained in:
+5
-2
@@ -88,9 +88,12 @@
|
|||||||
|
|
||||||
- [Patterns](ch18-00-patterns.md) (perhaps an appendix?)
|
- [Patterns](ch18-00-patterns.md) (perhaps an appendix?)
|
||||||
|
|
||||||
- [More Lifetimes](ch19-00-more-lifetimes.md) (perhaps merge this into advanced type system features?)
|
- [Advanced Features](ch19-00-advanced-features.md)
|
||||||
|
- [Unsafe Rust](ch19-01-unsafe-rust.md)
|
||||||
|
- [Advanced Lifetimes](ch19-02-advanced-lifetimes.md)
|
||||||
|
- [Advanced Traits](ch19-03-advanced-traits.md)
|
||||||
|
|
||||||
- [Advanced Type System Features](ch20-00-advanced-types.md) (perhaps called "Advanced Traits"?)
|
- [Un-named project](ch20-00-unnamed-project.md)
|
||||||
|
|
||||||
- [Appendix](appendix-00.md)
|
- [Appendix](appendix-00.md)
|
||||||
- [Keywords](appendix-01-keywords.md)
|
- [Keywords](appendix-01-keywords.md)
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
# Advanced Features
|
||||||
|
|
||||||
|
We've come a long way! By now, we've learned 99% of the things you'll need to
|
||||||
|
know when writing Rust. We'll wrap the book up by doing one more project, but
|
||||||
|
before we get to that, let's talk about a few things that you may run into that
|
||||||
|
last 1% of the time. Feel free to skip this chapter and come back to it once
|
||||||
|
you run into these things in the wild; the tools we'll learn to use here are
|
||||||
|
useful in very specific situations. We don't want to leave them out, but you
|
||||||
|
won't find yourself reaching for them often.
|
||||||
|
|
||||||
|
Here's a quick summary:
|
||||||
|
|
||||||
|
* Unsafe Rust: for when you need to tell Rust "just trust me, promise!"
|
||||||
|
* Advanced Lifetimes: Additional lifetime syntax for complex situations.
|
||||||
|
* Advanced Traits: Associated Types, coherence, and disambiguation.
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
# More Lifetimes
|
|
||||||
|
|
||||||
## Lifetimes that depend on other lifetimes
|
|
||||||
|
|
||||||
'a: 'b stuff: subtyping
|
|
||||||
|
|
||||||
## Higher ranked trait bounds
|
|
||||||
|
|
||||||
for<'a>
|
|
||||||
|
|
||||||
Needed for closures
|
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
# 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
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
# Advanced Lifetimes
|
||||||
|
|
||||||
|
### Lifetimes that depend on other lifetimes
|
||||||
|
|
||||||
|
'a: 'b stuff: subtyping
|
||||||
|
|
||||||
|
### Higher ranked trait bounds
|
||||||
|
|
||||||
|
for<'a>
|
||||||
|
|
||||||
|
Needed for closures
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
# Advanced 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
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
# 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
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
# Un-named project
|
||||||
Reference in New Issue
Block a user