提交 025c328b 编写于 作者: B bors

Auto merge of #39633 - steveklabnik:vendor-mdbook, r=alexcrichton

Port books to mdbook

Part of https://github.com/rust-lang/rust/issues/39588

blocked on https://github.com/rust-lang/rust/pull/39431

As a first step towards the bookshelf, we ~vendor mdbook in-tree and~ port our books to it. Eventually, both of these books will be moved out-of-tree, but the nightly book will rely on doing the same thing. As such, this intermediate step is useful.

r? @alexcrichton @brson

/cc @azerupi
......@@ -6,4 +6,4 @@
*.rs rust
src/etc/installer/gfx/* binary
*.woff binary
src/vendor/* binary
src/vendor/** -text
......@@ -100,3 +100,4 @@ version.md
version.ml
version.texi
.cargo
!src/vendor/**
......@@ -6,6 +6,14 @@ dependencies = [
"libc 0.0.0",
]
[[package]]
name = "aho-corasick"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "alloc"
version = "0.0.0"
......@@ -31,10 +39,25 @@ dependencies = [
"libc 0.0.0",
]
[[package]]
name = "ansi_term"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "arena"
version = "0.0.0"
[[package]]
name = "bitflags"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bitflags"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bootstrap"
version = "0.0.0"
......@@ -69,6 +92,21 @@ dependencies = [
name = "cargotest"
version = "0.1.0"
[[package]]
name = "clap"
version = "2.19.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
"strsim 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"term_size 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-segmentation 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cmake"
version = "0.1.18"
......@@ -114,6 +152,7 @@ version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -158,6 +197,33 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "graphviz"
version = "0.0.0"
[[package]]
name = "handlebars"
version = "0.20.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"pest 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"quick-error 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "kernel32-sys"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.0.0"
......@@ -183,6 +249,27 @@ name = "log"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "mdbook"
version = "0.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"clap 2.19.3 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"handlebars 0.20.5 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"pulldown-cmark 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "memchr"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num_cpus"
version = "0.2.13"
......@@ -209,6 +296,11 @@ dependencies = [
"unwind 0.0.0",
]
[[package]]
name = "pest"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "proc_macro"
version = "0.0.0"
......@@ -225,6 +317,15 @@ dependencies = [
"syntax_pos 0.0.0",
]
[[package]]
name = "pulldown-cmark"
version = "0.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "qemu-test-client"
version = "0.1.0"
......@@ -233,6 +334,11 @@ version = "0.1.0"
name = "qemu-test-server"
version = "0.1.0"
[[package]]
name = "quick-error"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rand"
version = "0.0.0"
......@@ -240,9 +346,30 @@ dependencies = [
"core 0.0.0",
]
[[package]]
name = "regex"
version = "0.1.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rustbook"
version = "0.0.0"
version = "0.1.0"
dependencies = [
"clap 2.19.3 (registry+https://github.com/rust-lang/crates.io-index)",
"mdbook 0.0.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rustc"
......@@ -650,6 +777,11 @@ dependencies = [
"core 0.0.0",
]
[[package]]
name = "strsim"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "syntax"
version = "0.0.0"
......@@ -685,6 +817,16 @@ dependencies = [
name = "term"
version = "0.0.0"
[[package]]
name = "term_size"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "test"
version = "0.0.0"
......@@ -700,6 +842,23 @@ dependencies = [
"test 0.0.0",
]
[[package]]
name = "thread-id"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tidy"
version = "0.1.0"
......@@ -712,14 +871,69 @@ dependencies = [
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-segmentation"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-width"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "utf8-ranges"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "vec_map"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66"
"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"
"checksum bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4f67931368edf3a9a51d29886d245f1c3db2f1ef0dcc9e35ff70341b78c10d23"
"checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d"
"checksum clap 2.19.3 (registry+https://github.com/rust-lang/crates.io-index)" = "95b78f3fe0fc94c13c731714363260e04b557a637166f33a4570d3189d642374"
"checksum cmake 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "0e5bcf27e097a184c1df4437654ed98df3d7a516e8508a6ba45d8b092bbdf283"
"checksum env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "15abd780e45b3ea4f76b4e9a26ff4843258dd8a3eed2775a0e7368c2e7936c2f"
"checksum filetime 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "5363ab8e4139b8568a6237db5248646e5a8a2f89bd5ccb02092182b11fd3e922"
"checksum gcc 0.3.43 (registry+https://github.com/rust-lang/crates.io-index)" = "c07c758b972368e703a562686adb39125707cc1ef3399da8c019fc6c2498a75d"
"checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685"
"checksum handlebars 0.20.5 (registry+https://github.com/rust-lang/crates.io-index)" = "07f9c1d28bcfb97143c95ed0667141677b2b5675c7ba3d5b81459ad43b1073bd"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "cf186d1a8aa5f5bee5fd662bc9c1b949e0259e1bcc379d1f006847b0080c7417"
"checksum libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)" = "044d1360593a78f5c8e5e710beccdc24ab71d1f01bc19a29bcacdba22e8475d8"
"checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054"
"checksum mdbook 0.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "a629cd0194bbd0340a70db83a94b27cf9881fac8c55e6a2db983addcb7114ee4"
"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
"checksum num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "cee7e88156f3f9e19bdd598f8d6c9db7bf4078f99f8381f43a55b09648d1a6e3"
"checksum pest 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0a6dda33d67c26f0aac90d324ab2eb7239c819fc7b2552fe9faa4fe88441edc8"
"checksum pulldown-cmark 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1058d7bb927ca067656537eec4e02c2b4b70eaaa129664c5b90c111e20326f41"
"checksum quick-error 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0aad603e8d7fb67da22dbdf1f4b826ce8829e406124109e73cf1b2454b93a71c"
"checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f"
"checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957"
"checksum rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)" = "6159e4e6e559c81bd706afe9c8fd68f547d3e851ce12e76b1de7914bab61691b"
"checksum strsim 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "67f84c44fbb2f91db7fef94554e6b2ac05909c9c0b0bc23bb98d3a1aebfe7f7c"
"checksum term_size 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3f7f5f3f71b0040cecc71af239414c23fd3c73570f5ff54cf50e03cef637f2a0"
"checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03"
"checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
"checksum toml 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)" = "0590d72182e50e879c4da3b11c6488dae18fccb1ae0c7a3eda18e16795844796"
"checksum unicode-segmentation 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c3bc443ded17b11305ffffe6b37e2076f328a5a8cb6aa877b1b98f77699e98b5"
"checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f"
"checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f"
"checksum vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cac5efe5cb0fa14ec2f84f83c701c562ee63f6dcc680861b21d65c682adfb05f"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
......@@ -47,6 +47,7 @@ pub fn rustbook(build: &Build, target: &str, name: &str) {
build.run(build.tool_cmd(&compiler, "rustbook")
.arg("build")
.arg(&src)
.arg("-d")
.arg(out));
}
......
% The Rust Programming Language
# The Rust Programming Language
Welcome! This book will teach you about the [Rust Programming Language][rust].
Rust is a systems programming language focused on three goals: safety, speed,
......
# Summary
[Introduction](README.md)
* [Getting Started](getting-started.md)
* [Tutorial: Guessing Game](guessing-game.md)
* [Syntax and Semantics](syntax-and-semantics.md)
......
% Advanced Linking
# Advanced Linking
The common cases of linking with Rust have been covered earlier in this book,
but supporting the range of linking possibilities made available by other
......
% Associated Constants
# Associated Constants
With the `associated_consts` feature, you can define constants like this:
......
% Associated Types
# Associated Types
Associated types are a powerful part of Rust’s type system. They’re related to
the idea of a ‘type family’, in other words, grouping multiple types together. That
......
% Attributes
# Attributes
Declarations can be annotated with ‘attributes’ in Rust. They look like this:
......
% Benchmark tests
# Benchmark tests
Rust supports benchmark tests, which can test the performance of your
code. Let's make our `src/lib.rs` look like this (comments elided):
......
% Bibliography
# Bibliography
This is a reading list of material relevant to Rust. It includes prior
research that has - at one time or another - influenced the design of
......
% Borrow and AsRef
# Borrow and AsRef
The [`Borrow`][borrow] and [`AsRef`][asref] traits are very similar, but
different. Here’s a quick refresher on what these two traits mean.
......
% Box Syntax and Patterns
# Box Syntax and Patterns
Currently the only stable way to create a `Box` is via the `Box::new` method.
Also it is not possible in stable Rust to destructure a `Box` in a match
......
% Casting Between Types
# Casting Between Types
Rust, with its focus on safety, provides two different ways of casting
different types between each other. The first, `as`, is for safe casts.
......@@ -8,7 +8,7 @@ most dangerous features of Rust!
# Coercion
Coercion between types is implicit and has no syntax of its own, but can
be spelled out with [`as`](#explicit-coercions).
be spelled out with [`as`](#Explicit%20coercions).
Coercion occurs in `let`, `const`, and `static` statements; in
function call arguments; in field values in struct initialization; and in a
......@@ -19,7 +19,7 @@ The most common case of coercion is removing mutability from a reference:
* `&mut T` to `&T`
An analogous conversion is to remove mutability from a
[raw pointer](raw-pointers.md):
[raw pointer](raw-pointers.html):
* `*mut T` to `*const T`
......@@ -29,7 +29,7 @@ References can also be coerced to raw pointers:
* `&mut T` to `*mut T`
Custom coercions may be defined using [`Deref`](deref-coercions.md).
Custom coercions may be defined using [`Deref`](deref-coercions.html).
Coercion is transitive.
......@@ -101,7 +101,7 @@ The semantics of numeric casts are:
## Pointer casts
Perhaps surprisingly, it is safe to cast [raw pointers](raw-pointers.md) to and
Perhaps surprisingly, it is safe to cast [raw pointers](raw-pointers.html) to and
from integers, and to cast between pointers to different types subject to
some constraints. It is only unsafe to dereference the pointer:
......
% Choosing your Guarantees
# Choosing your Guarantees
One important feature of Rust is that it lets us control the costs and guarantees
of a program.
......
% Closures
# Closures
Sometimes it is useful to wrap up a function and _free variables_ for better
clarity and reuse. The free variables that can be used come from the
......@@ -463,7 +463,7 @@ fn factory() -> &(Fn(i32) -> i32) {
Right. Because we have a reference, we need to give it a lifetime. But
our `factory()` function takes no arguments, so
[elision](lifetimes.html#lifetime-elision) doesn’t kick in here. Then what
[elision](lifetimes.html#Lifetime%20Elision) doesn’t kick in here. Then what
choices do we have? Try `'static`:
```rust,ignore
......
% Comments
# Comments
Now that we have some functions, it’s a good idea to learn about comments.
Comments are notes that you leave to other programmers to help explain things
......
% Compiler Plugins
# Compiler Plugins
# Introduction
## Introduction
`rustc` can load compiler plugins, which are user-provided libraries that
extend the compiler's behavior with new syntax extensions, lint checks, etc.
......@@ -127,7 +127,7 @@ enum. For a more involved macro example, see
## Tips and tricks
Some of the [macro debugging tips](macros.html#debugging-macro-code) are applicable.
Some of the [macro debugging tips](macros.html#Debugging%20macro%20code) are applicable.
You can use `syntax::parse` to turn token trees into
higher-level syntax elements like expressions:
......
% Concurrency
# Concurrency
Concurrency and parallelism are incredibly important topics in computer
science, and are also a hot topic in industry today. Computers are gaining more
......@@ -55,7 +55,7 @@ For sharing references across threads, Rust provides a wrapper type called
`Arc<T>`. `Arc<T>` implements `Send` and `Sync` if and only if `T` implements
both `Send` and `Sync`. For example, an object of type `Arc<RefCell<U>>` cannot
be transferred across threads because
[`RefCell`](choosing-your-guarantees.html#refcellt) does not implement
[`RefCell`](choosing-your-guarantees.html#RefCell%3CT%3E) does not implement
`Sync`, consequently `Arc<RefCell<U>>` would not implement `Send`.
These two traits allow you to use the type system to make strong guarantees
......@@ -126,7 +126,7 @@ closure only captures a _reference to `x`_. This is a problem, because the
thread may outlive the scope of `x`, leading to a dangling pointer.
To fix this, we use a `move` closure as mentioned in the error message. `move`
closures are explained in depth [here](closures.html#move-closures); basically
closures are explained in depth [here](closures.html#move%20closures); basically
they move variables from their environment into themselves.
```rust
......
% Conditional Compilation
# Conditional Compilation
Rust has a special attribute, `#[cfg]`, which allows you to compile code
based on a flag passed to the compiler. It has two forms:
......
% const and static
# const and static
Rust has a way of defining constants with the `const` keyword:
......
% Crates and Modules
# Crates and Modules
When a project starts getting large, it’s considered good software
engineering practice to split it up into a bunch of smaller pieces, and then
......
% Custom Allocators
# Custom Allocators
Allocating memory isn't always the easiest thing to do, and while Rust generally
takes care of this by default it often becomes necessary to customize how
......
% `Deref` coercions
# `Deref` coercions
The standard library provides a special trait, [`Deref`][deref]. It’s normally
used to overload `*`, the dereference operator:
......
% Documentation
# Documentation
Documentation is an important part of any software project, and it's
first-class in Rust. Let's talk about the tooling Rust gives you to
......
% Drop
# Drop
Now that we’ve discussed traits, let’s talk about a particular trait provided
by the Rust standard library, [`Drop`][drop]. The `Drop` trait provides a way
......
% Effective Rust
# Effective Rust
So you’ve learned how to write some Rust code. But there’s a difference between
writing *any* Rust code and writing *good* Rust code.
......
% Enums
# Enums
An `enum` in Rust is a type that represents data that is one of
several possible variants. Each variant in the `enum` can optionally
......
% Error Handling
# Error Handling
Like most programming languages, Rust encourages the programmer to handle
errors in a particular way. Generally speaking, error handling is divided into
......@@ -21,35 +21,35 @@ sum types and combinators, and try to motivate the way Rust does error handling
incrementally. As such, programmers with experience in other expressive type
systems may want to jump around.
* [The Basics](#the-basics)
* [Unwrapping explained](#unwrapping-explained)
* [The `Option` type](#the-option-type)
* [Composing `Option<T>` values](#composing-optiont-values)
* [The `Result` type](#the-result-type)
* [Parsing integers](#parsing-integers)
* [The `Result` type alias idiom](#the-result-type-alias-idiom)
* [A brief interlude: unwrapping isn't evil](#a-brief-interlude-unwrapping-isnt-evil)
* [Working with multiple error types](#working-with-multiple-error-types)
* [Composing `Option` and `Result`](#composing-option-and-result)
* [The limits of combinators](#the-limits-of-combinators)
* [Early returns](#early-returns)
* [The `try!` macro](#the-try-macro)
* [Defining your own error type](#defining-your-own-error-type)
* [Standard library traits used for error handling](#standard-library-traits-used-for-error-handling)
* [The `Error` trait](#the-error-trait)
* [The `From` trait](#the-from-trait)
* [The real `try!` macro](#the-real-try-macro)
* [Composing custom error types](#composing-custom-error-types)
* [Advice for library writers](#advice-for-library-writers)
* [Case study: A program to read population data](#case-study-a-program-to-read-population-data)
* [Initial setup](#initial-setup)
* [Argument parsing](#argument-parsing)
* [Writing the logic](#writing-the-logic)
* [Error handling with `Box<Error>`](#error-handling-with-boxerror)
* [Reading from stdin](#reading-from-stdin)
* [Error handling with a custom type](#error-handling-with-a-custom-type)
* [Adding functionality](#adding-functionality)
* [The short story](#the-short-story)
* [The Basics](#The%20Basics)
* [Unwrapping explained](#Unwrapping%20explained)
* [The `Option` type](#The%20Option%20type)
* [Composing `Option<T>` values](#Composing%20Option%3CT%3E%20values)
* [The `Result` type](#The%20Result%20type)
* [Parsing integers](#Parsing%20integers)
* [The `Result` type alias idiom](#The%20Result%20type%20alias%20idiom)
* [A brief interlude: unwrapping isn't evil](#A%20brief%20interlude:%20unwrapping%20isnt%20evil)
* [Working with multiple error types](#Working%20with%20multiple%20error%20types)
* [Composing `Option` and `Result`](#Composing%20Option%20and%20Result)
* [The limits of combinators](#The%20limits%20of%20combinators)
* [Early returns](#Early%20returns)
* [The `try!` macro](#The%20try%20macro)
* [Defining your own error type](#Defining%20your%20own%20error%20type)
* [Standard library traits used for error handling](#Standard%20library%20traits%20used%20for%20error%20handling)
* [The `Error` trait](#The%20Error%20trait)
* [The `From` trait](#The%20From%20trait)
* [The real `try!` macro](#The%20real%20try%20macro)
* [Composing custom error types](#Composing%20custom%20error%20types)
* [Advice for library writers](#Advice%20for%20library%20writers)
* [Case study: A program to read population data](#Case%20study:%20A%20program%20to%20read%20population%20data)
* [Initial setup](#Initial%20setup)
* [Argument parsing](#Argument%20parsing)
* [Writing the logic](#Writing%20the%20logic)
* [Error handling with `Box<Error>`](#Error%20handling%20with%20Box%3CError%3E)
* [Reading from stdin](#Reading%20from%20stdin)
* [Error handling with a custom type](#Error%20handling%20with%20a%20custom%20type)
* [Adding functionality](#Adding%20functionality)
* [The short story](#The%20short%20story)
# The Basics
......@@ -796,7 +796,7 @@ because of the return types of
[`std::fs::File::open`](../std/fs/struct.File.html#method.open) and
[`std::io::Read::read_to_string`](../std/io/trait.Read.html#method.read_to_string).
(Note that they both use the [`Result` type alias
idiom](#the-result-type-alias-idiom) described previously. If you
idiom](#The%20Result%20type%20alias%20idiom) described previously. If you
click on the `Result` type, you'll [see the type
alias](../std/io/type.Result.html), and consequently, the underlying
`io::Error` type.) The third problem is described by the
......@@ -1120,7 +1120,7 @@ returns an `&Error`, which is itself a trait object. We'll revisit the
For now, it suffices to show an example implementing the `Error` trait. Let's
use the error type we defined in the
[previous section](#defining-your-own-error-type):
[previous section](#Defining%20your%20own%20error%20type):
```rust
use std::io;
......@@ -1493,19 +1493,19 @@ representation. But certainly, this will vary depending on use cases.
At a minimum, you should probably implement the
[`Error`](../std/error/trait.Error.html)
trait. This will give users of your library some minimum flexibility for
[composing errors](#the-real-try-macro). Implementing the `Error` trait also
[composing errors](#The%20real%20try%20macro). Implementing the `Error` trait also
means that users are guaranteed the ability to obtain a string representation
of an error (because it requires impls for both `fmt::Debug` and
`fmt::Display`).
Beyond that, it can also be useful to provide implementations of `From` on your
error types. This allows you (the library author) and your users to
[compose more detailed errors](#composing-custom-error-types). For example,
[compose more detailed errors](#Composing%20custom%20error%20types). For example,
[`csv::Error`](http://burntsushi.net/rustdoc/csv/enum.Error.html)
provides `From` impls for both `io::Error` and `byteorder::Error`.
Finally, depending on your tastes, you may also want to define a
[`Result` type alias](#the-result-type-alias-idiom), particularly if your
[`Result` type alias](#The%20Result%20type%20alias%20idiom), particularly if your
library defines a single error type. This is used in the standard library
for [`io::Result`](../std/io/type.Result.html)
and [`fmt::Result`](../std/fmt/type.Result.html).
......@@ -1538,7 +1538,7 @@ and [`rustc-serialize`](https://crates.io/crates/rustc-serialize) crates.
We're not going to spend a lot of time on setting up a project with
Cargo because it is already covered well in [the Cargo
section](getting-started.html#hello-cargo) and [Cargo's documentation][14].
section](getting-started.html#Hello%20Cargo) and [Cargo's documentation][14].
To get started from scratch, run `cargo new --bin city-pop` and make sure your
`Cargo.toml` looks something like this:
......@@ -1729,7 +1729,7 @@ error types and you don't need any `From` implementations. The downside is that
since `Box<Error>` is a trait object, it *erases the type*, which means the
compiler can no longer reason about its underlying type.
[Previously](#the-limits-of-combinators) we started refactoring our code by
[Previously](#The%20limits%20of%20combinators) we started refactoring our code by
changing the type of our function from `T` to `Result<T, OurErrorType>`. In
this case, `OurErrorType` is only `Box<Error>`. But what's `T`? And can we add
a return type to `main`?
......
% Foreign Function Interface
# Foreign Function Interface
# Introduction
......@@ -680,7 +680,7 @@ pub extern fn hello_rust() -> *const u8 {
The `extern` makes this function adhere to the C calling convention, as
discussed above in "[Foreign Calling
Conventions](ffi.html#foreign-calling-conventions)". The `no_mangle`
Conventions](ffi.html#Foreign%20calling%20conventions)". The `no_mangle`
attribute turns off Rust's name mangling, so that it is easier to link to.
# FFI and panics
......
% Functions
# Functions
Every Rust program has at least one function, the `main` function:
......
% Generics
# Generics
Sometimes, when writing a function or data type, we may want it to work for
multiple types of arguments. In Rust, we can do this with generics.
......
% Getting Started
# Getting Started
This first chapter of the book will get us going with Rust and its tooling.
First, we’ll install Rust. Then, the classic ‘Hello World’ program. Finally,
......@@ -236,7 +236,7 @@ language]*, which means that most things are expressions, rather than
statements. The `;` indicates that this expression is over, and the next one is
ready to begin. Most lines of Rust code end with a `;`.
[expression-oriented language]: glossary.html#expression-oriented-language
[expression-oriented language]: glossary.html#Expression-Oriented%20Language
## Compiling and Running Are Separate Steps
......
% Glossary
# Glossary
Not every Rustacean has a background in systems programming, nor in computer
science, so we've added explanations of terms that might be unfamiliar.
......@@ -56,7 +56,7 @@ They can be used to manage control flow in a modular fashion.
A type without a statically known size or alignment. ([more info][link])
[link]: ../nomicon/exotic-sizes.html#dynamically-sized-types-dsts
[link]: ../nomicon/exotic-sizes.html#Dynamically%20Sized%20Types%20(DSTs)
### Expression
......@@ -76,8 +76,8 @@ In an expression-oriented language, (nearly) every statement is an expression
and therefore returns a value. Consequently, these expression statements can
themselves form part of larger expressions.
[expression]: glossary.html#expression
[statement]: glossary.html#statement
[expression]: glossary.html#Expression
[statement]: glossary.html#Statement
### Statement
......
% Guessing Game
# Guessing Game
Let’s learn some Rust! For our first project, we’ll implement a classic
beginner programming problem: the guessing game. Here’s how it works: Our
......@@ -119,7 +119,7 @@ there are no arguments, and `{` starts the body of the function. Because
we didn’t include a return type, it’s assumed to be `()`, an empty
[tuple][tuples].
[tuples]: primitive-types.html#tuples
[tuples]: primitive-types.html#Tuples
```rust,ignore
println!("Guess the number!");
......@@ -727,7 +727,7 @@ thirty-two bit integer. Rust has [a number of built-in number types][number],
but we’ve chosen `u32`. It’s a good default choice for a small positive number.
[parse]: ../std/primitive.str.html#method.parse
[number]: primitive-types.html#numeric-types
[number]: primitive-types.html#Numeric%20types
Just like `read_line()`, our call to `parse()` could cause an error. What if
our string contained `A👍%`? There’d be no way to convert that to a number. As
......
% if let
# if let
`if let` permits [patterns][pattern] matching within the condition of an [if][if] statement.
This allows us to reduce the overhead of certain kinds of [pattern][patterns] matches
......
% if
# if
Rust’s take on `if` is not particularly complex, but it’s much more like the
`if` you’ll find in a dynamically typed language than in a more traditional
......
% Inline Assembly
# Inline Assembly
For extremely low-level manipulations and performance reasons, one
might wish to control the CPU directly. Rust supports using inline
......
% Intrinsics
# Intrinsics
> **Note**: intrinsics will forever have an unstable interface, it is
> recommended to use the stable interfaces of libcore rather than intrinsics
......
% Lang items
# Lang items
> **Note**: lang items are often provided by crates in the Rust distribution,
> and lang items themselves have an unstable interface. It is recommended to use
......
% Lifetimes
# Lifetimes
This is the last of three sections presenting Rust’s ownership system. This is one of
Rust’s most distinct and compelling features, with which Rust developers should
......@@ -139,7 +139,7 @@ associated with it, but the compiler lets you elide (i.e. omit, see
["Lifetime Elision"][lifetime-elision] below) them in common cases. Before we
get to that, though, let’s look at a short example with explicit lifetimes:
[lifetime-elision]: #lifetime-elision
[lifetime-elision]: #Lifetime%20Elision
```rust,ignore
fn bar<'a>(...)
......
% Loops
# Loops
Rust currently provides three approaches to performing some kind of iterative activity. They are: `loop`, `while` and `for`. Each approach has its own set of uses.
......
% Macros
# Macros
By now you’ve learned about many of the tools Rust provides for abstracting and
reusing code. These units of code reuse have a rich semantic structure. For
......@@ -432,7 +432,7 @@ Even when Rust code contains un-expanded macros, it can be parsed as a full
tools that process code. It also has a few consequences for the design of
Rust’s macro system.
[ast]: glossary.html#abstract-syntax-tree
[ast]: glossary.html#Abstract%20Syntax%20Tree
One consequence is that Rust must determine, when it parses a macro invocation,
whether the macro stands in for
......
% Match
# Match
Often, a simple [`if`][if]/`else` isn’t enough, because you have more than two
possible options. Also, conditions can get quite complex. Rust
......
% Method Syntax
# Method Syntax
Functions are great, but if you want to call a bunch of them on some data, it
can be awkward. Consider this code:
......
% Mutability
# Mutability
Mutability, the ability to change something, works a bit differently in Rust
than in other languages. The first aspect of mutability is its non-default
......@@ -89,7 +89,7 @@ philosophy, memory safety, and the mechanism by which Rust guarantees it, the
> * exactly one mutable reference (`&mut T`).
[ownership]: ownership.html
[borrowing]: references-and-borrowing.html#borrowing
[borrowing]: references-and-borrowing.html#Borrowing
So, that’s the real definition of ‘immutability’: is this safe to have two
pointers to? In `Arc<T>`’s case, yes: the mutation is entirely contained inside
......
% Nightly Rust
# Nightly Rust
Rust provides three distribution channels for Rust: nightly, beta, and stable.
Unstable features are only available on nightly Rust. For more details on this
process, see [Stability as a deliverable][stability].
process, see [Stability as a deliverable][stability].
[stability]: http://blog.rust-lang.org/2014/10/30/Stability.html
......@@ -91,9 +91,10 @@ If not, there are a number of places where you can get help. The easiest is
[the #rust IRC channel on irc.mozilla.org][irc], which you can access through
[Mibbit][mibbit]. Click that link, and you'll be chatting with other Rustaceans
(a silly nickname we call ourselves), and we can help you out. Other great
resources include [the users forum][users], and [Stack Overflow][stackoverflow].
resources include [the users forum][users], and [Stack Overflow][stackoverflow].
[irc]: irc://irc.mozilla.org/#rust
[mibbit]: http://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust
[users]: https://users.rust-lang.org/
[stackoverflow]: http://stackoverflow.com/questions/tagged/rust
% No stdlib
# No stdlib
Rust’s standard library provides a lot of useful functionality, but assumes
support for various features of its host system: threads, networking, heap
......
% Operators and Overloading
# Operators and Overloading
Rust allows for a limited form of operator overloading. There are certain
operators that are able to be overloaded. To support a particular operator
......
% Ownership
# Ownership
This is the first of three sections presenting Rust’s ownership system. This is one of
Rust’s most distinct and compelling features, with which Rust developers should
......@@ -65,10 +65,10 @@ elements onto them.
Vectors have a [generic type][generics] `Vec<T>`, so in this example `v` will have type
`Vec<i32>`. We'll cover [generics] in detail in a later chapter.
[arrays]: primitive-types.html#arrays
[arrays]: primitive-types.html#Arrays
[vectors]: vectors.html
[heap]: the-stack-and-the-heap.html#the-heap
[stack]: the-stack-and-the-heap.html#the-stack
[heap]: the-stack-and-the-heap.html#The%20Heap
[stack]: the-stack-and-the-heap.html#The%20Stack
[bindings]: variable-bindings.html
[generics]: generics.html
......@@ -136,7 +136,7 @@ Rust allocates memory for an integer [i32] on the [stack][sh], copies the bit
pattern representing the value of 10 to the allocated memory and binds the
variable name x to this memory region for future reference.
[i32]: primitive-types.html#numeric-types
[i32]: primitive-types.html#Numeric%20types
Now consider the following code fragment:
......
% Patterns
# Patterns
Patterns are quite common in Rust. We use them in [variable
bindings][bindings], [match expressions][match], and other places, too. Let’s go
......
% Primitive Types
# Primitive Types
The Rust language has a number of types that are considered ‘primitive’. This
means that they’re built-in to the language. Rust is structured in such a way
......@@ -232,7 +232,7 @@ soon.
You can assign one tuple into another, if they have the same contained types
and [arity]. Tuples have the same arity when they have the same length.
[arity]: glossary.html#arity
[arity]: glossary.html#Arity
```rust
let mut x = (1, 2); // x: (i32, i32)
......
% Procedural Macros (and custom Derive)
# Procedural Macros (and custom Derive)
As you've seen throughout the rest of the book, Rust provides a mechanism
called "derive" that lets you implement traits easily. For example,
......
% Raw Pointers
# Raw Pointers
Rust has a number of different smart pointer types in its standard library, but
there are two types that are extra-special. Much of Rust’s safety comes from
......
% References and Borrowing
# References and Borrowing
This is the second of three sections presenting Rust’s ownership system. This is one of
Rust’s most distinct and compelling features, with which Rust developers should
......
% Release Channels
# Release Channels
The Rust project uses a concept called ‘release channels’ to manage releases.
It’s important to understand this process to choose which version of Rust
......
% Slice patterns
# Slice patterns
If you want to match against a slice or array, you can use `&` with the
`slice_patterns` feature:
......
% Strings
# Strings
Strings are an important concept for any programmer to master. Rust’s string
handling system is a bit different from other languages, due to its systems
......
% Structs
# Structs
`struct`s are a way of creating more complex data types. For example, if we were
doing calculations involving coordinates in 2D space, we would need both an `x`
......
% Syntax and Semantics
# Syntax and Semantics
This chapter breaks Rust down into small chunks, one for each concept.
......
% Syntax Index
# Syntax Index
## Keywords
......@@ -196,18 +196,18 @@
[Associated Types]: associated-types.html
[Attributes]: attributes.html
[Casting Between Types (`as`)]: casting-between-types.html#as
[Closures (`move` closures)]: closures.html#move-closures
[Closures (`move` closures)]: closures.html#move%20closures
[Closures]: closures.html
[Comments]: comments.html
[Crates and Modules (Defining Modules)]: crates-and-modules.html#defining-modules
[Crates and Modules (Exporting a Public Interface)]: crates-and-modules.html#exporting-a-public-interface
[Crates and Modules (Importing External Crates)]: crates-and-modules.html#importing-external-crates
[Crates and Modules (Importing Modules with `use`)]: crates-and-modules.html#importing-modules-with-use
[Crates and Modules (Re-exporting with `pub use`)]: crates-and-modules.html#re-exporting-with-pub-use
[Diverging Functions]: functions.html#diverging-functions
[Crates and Modules (Defining Modules)]: crates-and-modules.html#Defining%20modules
[Crates and Modules (Exporting a Public Interface)]: crates-and-modules.html#Exporting%20a%20public%20interface
[Crates and Modules (Importing External Crates)]: crates-and-modules.html#Importing%20external%20crates
[Crates and Modules (Importing Modules with `use`)]: crates-and-modules.html#Importing%20modules%20with%20use
[Crates and Modules (Re-exporting with `pub use`)]: crates-and-modules.html#Re-exporting%20with%20pub%20use
[Diverging Functions]: functions.html#Diverging%20functions
[Enums]: enums.html
[Foreign Function Interface]: ffi.html
[Functions (Early Returns)]: functions.html#early-returns
[Functions (Early Returns)]: functions.html#Early%20returns
[Functions]: functions.html
[Generics]: generics.html
[Iterators]: iterators.html
......@@ -216,24 +216,24 @@
[Loops (`for`)]: loops.html#for
[Loops (`loop`)]: loops.html#loop
[Loops (`while`)]: loops.html#while
[Loops (Ending Iteration Early)]: loops.html#ending-iteration-early
[Loops (Loops Labels)]: loops.html#loop-labels
[Loops (Ending Iteration Early)]: loops.html#Ending%20iteration%20early
[Loops (Loops Labels)]: loops.html#Loop%20labels
[Macros]: macros.html
[Match]: match.html
[Method Syntax (Method Calls)]: method-syntax.html#method-calls
[Method Syntax (Method Calls)]: method-syntax.html#Method%20calls
[Method Syntax]: method-syntax.html
[Mutability]: mutability.html
[Operators and Overloading]: operators-and-overloading.html
[Patterns (`ref` and `ref mut`)]: patterns.html#ref-and-ref-mut
[Patterns (Bindings)]: patterns.html#bindings
[Patterns (Ignoring bindings)]: patterns.html#ignoring-bindings
[Patterns (Multiple patterns)]: patterns.html#multiple-patterns
[Patterns (Ranges)]: patterns.html#ranges
[Patterns (`ref` and `ref mut`)]: patterns.html#ref%20and%20ref%20mut
[Patterns (Bindings)]: patterns.html#Bindings
[Patterns (Ignoring bindings)]: patterns.html#Ignoring%20bindings
[Patterns (Multiple patterns)]: patterns.html#Multiple%20patterns
[Patterns (Ranges)]: patterns.html#Ranges
[Primitive Types (`char`)]: primitive-types.html#char
[Primitive Types (Arrays)]: primitive-types.html#arrays
[Primitive Types (Booleans)]: primitive-types.html#booleans
[Primitive Types (Tuple Indexing)]: primitive-types.html#tuple-indexing
[Primitive Types (Tuples)]: primitive-types.html#tuples
[Primitive Types (Arrays)]: primitive-types.html#Arrays
[Primitive Types (Booleans)]: primitive-types.html#Booleans
[Primitive Types (Tuple Indexing)]: primitive-types.html#Tuple%20indexing
[Primitive Types (Tuples)]: primitive-types.html#Tuples
[Raw Pointers]: raw-pointers.html
[Reference (Byte String Literals)]: ../reference.html#byte-string-literals
[Reference (Integer literals)]: ../reference.html#integer-literals
......@@ -241,13 +241,13 @@
[Reference (Raw String Literals)]: ../reference.html#raw-string-literals
[References and Borrowing]: references-and-borrowing.html
[Strings]: strings.html
[Structs (Update syntax)]: structs.html#update-syntax
[Structs (Update syntax)]: structs.html#Update%20syntax
[Structs]: structs.html
[Traits (`where` clause)]: traits.html#where-clause
[Traits (Multiple Trait Bounds)]: traits.html#multiple-trait-bounds
[Traits (`where` clause)]: traits.html#Where%20clause
[Traits (Multiple Trait Bounds)]: traits.html#Multiple%20trait%20bounds
[Traits]: traits.html
[Universal Function Call Syntax]: ufcs.html
[Universal Function Call Syntax (Angle-bracket Form)]: ufcs.html#angle-bracket-form
[Universal Function Call Syntax (Angle-bracket Form)]: ufcs.html#Angle-bracket%20Form
[Unsafe]: unsafe.html
[Unsized Types (`?Sized`)]: unsized-types.html#sized
[Unsized Types (`?Sized`)]: unsized-types.html#Sized
[Variable Bindings]: variable-bindings.html
% Testing
# Testing
> Program testing can be a very effective way to show the presence of bugs, but
> it is hopelessly inadequate for showing their absence.
......
% The Stack and the Heap
# The Stack and the Heap
As a systems language, Rust operates at a low level. If you’re coming from a
high-level language, there are some aspects of systems programming that you may
......
% Trait Objects
# Trait Objects
When code involves polymorphism, there needs to be a mechanism to determine
which specific version is actually run. This is called ‘dispatch’. There are
......
% Traits
# Traits
A trait is a language feature that tells the Rust compiler about
functionality a type must provide.
......@@ -81,7 +81,7 @@ Traits are useful because they allow a type to make certain promises about its
behavior. Generic functions can exploit this to constrain, or [bound][bounds], the types they
accept. Consider this function, which does not compile:
[bounds]: glossary.html#bounds
[bounds]: glossary.html#Bounds
```rust,ignore
fn print_area<T>(shape: T) {
......
% Type Aliases
# Type Aliases
The `type` keyword lets you declare an alias of another type:
......@@ -55,7 +55,7 @@ if x == y {
This compiles without error. Values of a `Num` type are the same as a value of
type `i32`, in every way. You can use [tuple struct] to really get a new type.
[tuple struct]: structs.html#tuple-structs
[tuple struct]: structs.html#Tuple%20structs
You can also use type aliases with generics:
......
% Universal Function Call Syntax
# Universal Function Call Syntax
Sometimes, functions can have the same names. Consider this code:
......
% Unsafe
# Unsafe
Rust’s main draw is its powerful static guarantees about behavior. But safety
checks are conservative by nature: there are some programs that are actually
......
% Unsized Types
# Unsized Types
Most types have a particular size, in bytes, that is knowable at compile time.
For example, an `i32` is thirty-two bits big, or four bytes. However, there are
......
% Using Rust Without the Standard Library
# Using Rust Without the Standard Library
Rust’s standard library provides a lot of useful functionality, but assumes
support for various features of its host system: threads, networking, heap
......
% Variable Bindings
# Variable Bindings
Virtually every non-'Hello World’ Rust program uses *variable bindings*. They
bind some value to a name, so it can be used later. `let` is
......
% Vectors
# Vectors
A ‘vector’ is a dynamic or ‘growable’ array, implemented as the standard
library type [`Vec<T>`][vec]. The `T` means that we can have vectors
......@@ -151,6 +151,6 @@ API documentation][vec].
[vec]: ../std/vec/index.html
[box]: ../std/boxed/index.html
[generic]: generics.html
[panic]: concurrency.html#panics
[panic]: concurrency.html#Panics
[get]: ../std/vec/struct.Vec.html#method.get
[get_mut]: ../std/vec/struct.Vec.html#method.get_mut
% The Rustonomicon
# The Rustonomicon
#### The Dark Arts of Advanced and Unsafe Rust Programming
......
# Summary
[Introduction](README.md)
* [Meet Safe and Unsafe](meet-safe-and-unsafe.md)
* [How Safe and Unsafe Interact](safe-unsafe-meaning.md)
* [Working with Unsafe](working-with-unsafe.md)
......
% Implementing Arc and Mutex
# Implementing Arc and Mutex
Knowing the theory is all fine and good, but the *best* way to understand
something is to use it. To better understand atomics and interior mutability,
......
% Atomics
# Atomics
Rust pretty blatantly just inherits C11's memory model for atomics. This is not
due to this model being particularly excellent or easy to understand. Indeed,
......
% Splitting Borrows
# Splitting Borrows
The mutual exclusion property of mutable references can be very limiting when
working with a composite structure. The borrow checker understands some basic
......
% Casts
# Casts
Casts are a superset of coercions: every coercion can be explicitly
invoked via a cast. However some conversions require a cast.
......
% Checked Uninitialized Memory
# Checked Uninitialized Memory
Like C, all stack variables in Rust are uninitialized until a value is
explicitly assigned to them. Unlike C, Rust statically prevents you from ever
......
% Coercions
# Coercions
Types can implicitly be coerced to change in certain contexts. These changes are
generally just *weakening* of types, largely focused around pointers and
......
% Concurrency and Parallelism
# Concurrency and Parallelism
Rust as a language doesn't *really* have an opinion on how to do concurrency or
parallelism. The standard library exposes OS threads and blocking sys-calls
......
% Constructors
# Constructors
There is exactly one way to create an instance of a user-defined type: name it,
and initialize all its fields at once:
......
% Type Conversions
# Type Conversions
At the end of the day, everything is just a pile of bits somewhere, and type
systems are just there to help us use those bits right. There are two common
......
% Data Representation in Rust
# Data Representation in Rust
Low-level programming cares a lot about data layout. It's a big deal. It also
pervasively influences the rest of the language, so we're going to start by
......
% Destructors
# Destructors
What the language *does* provide is full-blown automatic destructors through the
`Drop` trait, which provides the following method:
......
% The Dot Operator
# The Dot Operator
The dot operator will perform a lot of magic to convert types. It will perform
auto-referencing, auto-dereferencing, and coercion until types match.
......
% Drop Flags
# Drop Flags
The examples in the previous section introduce an interesting problem for Rust.
We have seen that it's possible to conditionally initialize, deinitialize, and
......
% Drop Check
# Drop Check
We have seen how lifetimes provide us some fairly simple rules for ensuring
that we never read dangling references. However up to this point we have only ever
......
% Exception Safety
# Exception Safety
Although programs should use unwinding sparingly, there's a lot of code that
*can* panic. If you unwrap a None, index out of bounds, or divide by 0, your
......
% Exotically Sized Types
# Exotically Sized Types
Most of the time, we think in terms of types with a fixed, positive size. This
is not always the case, however.
......
% Higher-Rank Trait Bounds (HRTBs)
# Higher-Rank Trait Bounds (HRTBs)
Rust's `Fn` traits are a little bit magic. For instance, we can write the
following code:
......
% Leaking
# Leaking
Ownership-based resource management is intended to simplify composition. You
acquire resources when you create the object, and you release the resources when
......
% Lifetime Elision
# Lifetime Elision
In order to make common patterns more ergonomic, Rust allows lifetimes to be
*elided* in function signatures.
......
% Limits of Lifetimes
# Limits of Lifetimes
Given the following code:
......@@ -78,4 +78,4 @@ TODO: other common problems? SEME regions stuff, mostly?
[ex2]: lifetimes.html#example-aliasing-a-mutable-reference
[ex2]: lifetimes.html#Example%3A%20aliasing%20a%20mutable%20reference
% Lifetimes
# Lifetimes
Rust enforces these rules through *lifetimes*. Lifetimes are effectively
just names for scopes somewhere in the program. Each reference,
......
% Meet Safe and Unsafe
# Meet Safe and Unsafe
Programmers in safe "high-level" languages face a fundamental dilemma. On one
hand, it would be *really* great to just say what you want and not worry about
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册