Skip to content

Commit 5db5010

Browse files
authored
Merge pull request #225 from nrc/intro
New intro draft; shuffle contents around
2 parents 8591643 + 275e5be commit 5db5010

File tree

7 files changed

+101
-3
lines changed

7 files changed

+101
-3
lines changed

README.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
# async-book
2-
Asynchronous Programming in Rust
1+
# Asynchronous Programming in Rust
2+
3+
This book aims to be a thorough guide to asynchronous programming in Rust, from beginner to advanced.
4+
5+
This book has been unmaintained for a long time and has not had a lot of love. We're currently working to bring it up to date and make it much better! As we're making some major changes, the content might be a bit mixed up, parts may be duplicated or missing, etc. Bear with us, it'll get better soon :-) To see what we're planning and to let us know what you think, see [issue 224](https://github.com/rust-lang/async-book/issues/224).
36

47
## Requirements
5-
The async book is built with [`mdbook`], you can install it using cargo.
8+
9+
The async book is built with [`mdbook`] ([docs](https://rust-lang.github.io/mdBook/index.html)), you can install it using cargo.
610

711
```
812
cargo install mdbook
@@ -12,14 +16,18 @@ cargo install mdbook-linkcheck
1216
[`mdbook`]: https://github.com/rust-lang/mdBook
1317

1418
## Building
19+
1520
To create a finished book, run `mdbook build` to generate it under the `book/` directory.
21+
1622
```
1723
mdbook build
1824
```
1925

2026
## Development
27+
2128
While writing it can be handy to see your changes, `mdbook serve` will launch a local web
2229
server to serve the book.
30+
2331
```
2432
mdbook serve
2533
```

ci/dictionary.txt

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ FutOne
2626
FutTwo
2727
FuturesUnordered
2828
GenFuture
29+
GitHub
2930
gRPC
3031
html
3132
http
@@ -65,6 +66,7 @@ ReadIntoBuf
6566
recognise
6667
refactor
6768
RefCell
69+
repo
6870
repurposed
6971
requeue
7072
ResponseFuture
@@ -102,6 +104,7 @@ Waker
102104
waker
103105
Wakeups
104106
wakeups
107+
webpage
105108
webpages
106109
webserver
107110
Woot

examples/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[workspace]
22
members = [
3+
"hello-world",
34
"01_02_why_async",
45
"01_04_async_await_primer",
56
"02_02_future_trait",
@@ -18,3 +19,4 @@ members = [
1819
"09_04_concurrent_tcp_server",
1920
"09_05_final_tcp_server",
2021
]
22+
resolver = "2"

examples/hello-world/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "hello-world"
3+
version = "0.1.0"
4+
authors = ["Nicholas Cameron <[email protected]>"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
tokio = { version = "1.40.0", features = ["full"] }

examples/hello-world/src/main.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Define an async function.
2+
async fn say_hello() {
3+
println!("hello, world!");
4+
}
5+
6+
#[tokio::main] // Boilerplate which lets us write `async fn main`, we'll explain it later.
7+
async fn main() {
8+
// Call an async function and await its result.
9+
say_hello().await;
10+
}

src/SUMMARY.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Table of Contents
22

3+
[Introduction](intro.md)
4+
5+
- [Navigation]()
6+
- [By topic]()
7+
- [FAQs]()
8+
- [Index]()
9+
10+
# Part 1: guide
11+
12+
# Part 2: reference
13+
14+
# Old chapters
15+
316
- [Getting Started](01_getting_started/01_chapter.md)
417
- [Why Async?](01_getting_started/02_why_async.md)
518
- [The State of Asynchronous Rust](01_getting_started/03_state_of_async_rust.md)
@@ -35,3 +48,4 @@
3548
- [TODO: Modeling Servers and the Request/Response Pattern]()
3649
- [TODO: Managing Shared State]()
3750
- [Appendix: Translations of the Book](12_appendix/01_translations.md)
51+

src/intro.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
NOTE: this guide is currently undergoing a rewrite after a long time without much work. It is work in progress, much is missing, and what exists is a bit rough.
2+
3+
# Introduction
4+
5+
This book is a guide to asynchronous programming in Rust. It is designed to help you take your first steps and to discover more about advanced topics. We don't assume any experience with asynchronous programming (in Rust or another language), but we do assume you're familiar with Rust already. If you want to learn about Rust, you could start with [The Rust Programming Language](https://doc.rust-lang.org/stable/book/).
6+
7+
This book has two main parts: part one is a beginners guide, it is designed to be read in-order and to take you from total beginner to intermediate level. Part two is a collection of stand-alone chapters on more advanced topics. It should be useful once you've worked through part one or if you already have some experience with async Rust.
8+
9+
You can navigate this book in multiple ways:
10+
11+
* You can read it front to back, in order. This is the recommend path for newcomers to async Rust, at least for part one of the book.
12+
* There is a summary contents on the left-hand side of the webpage.
13+
* If you want information about a broad topic, you could start with the topic index.
14+
* If you want to find all discussion about a specific topic, you could start with the detailed index.
15+
* You could see if your question is answered in the FAQs.
16+
17+
18+
## What is Async Programming?
19+
20+
In concurrent programming, the program does multiple things at the same time (or at least appears to). Programming with threads is one form of concurrent programming. Code within a thread is written in sequential style and the operating system executes threads concurrently. With async programming, concurrency happens entirely within your program (the operating system is not involved). An async runtime (which is just another crate in Rust) manages async tasks in conjunction with the programmer explicitly yielding control by using the `await` keyword.
21+
22+
Because the operating system is not involved, *context switching* in the async world is very fast. Furthermore, async tasks have much lower memory overhead than operating system threads. This makes async programming a good fit for systems which need to handle very many concurrent tasks and where those tasks spend a lot of time waiting (for example, for client responses or for IO).
23+
24+
Async programming also offers the programmer fine-grained control over how tasks are executed (levels of parallelism and concurrency, control flow, scheduling, and so forth). This means that async programming can be expressive as well as ergonomic for many uses.
25+
26+
27+
## Hello, world!
28+
29+
Just to give you a taste of what async Rust looks like, here is a 'hello, world' example. There is no concurrency, and it doesn't really take advantage of being async. It does define and use an async function, and it does print "hello, world!":
30+
31+
```rust,edition2021
32+
{{#include ../examples/hello-world/src/main.rs}}
33+
```
34+
35+
We'll explain everything in detail later. For now, note how we define an asynchronous function using `async fn` and call it using `.await` - an async function in Rust doesn't do anything unless it is `await`ed[^blocking].
36+
37+
Like all examples in this book, if you want to see the full example (including `Cargo.toml`, for example) or to run it yourself locally, you can find them in the book's GitHub repo: e.g., [examples/hello-world]().
38+
39+
TODO link: https://github.com/rust-lang/async-book/tree/master/examples/hello-world
40+
41+
42+
## Development of Async Rust
43+
44+
The async features of Rust have been in development for a while, but it is not a 'finished' part of the language. Async Rust (at least the parts available in the stable compiler and standard libraries) is reliable and performant. It is used in production in some of the most demanding situations at the largest tech companies. However, there are some missing parts and rough edges (rough in the sense of ergonomics rather than reliability). You are likely to stumble upon some of these parts during your journey with async Rust. For most missing parts, there are workarounds and these are covered in this book.
45+
46+
Currently, working with async iterators (also known as streams) is where most users find some rough parts. Some uses of async in traits are not yet well-supported. Async closures don't exist yet, and there is not a good solution for async destruction.
47+
48+
Async Rust is being actively worked on. If you want to follow development, you can check out the Async Working Group's [home page](https://rust-lang.github.io/wg-async/meetings.html) which includes their [roadmap](https://rust-lang.github.io/wg-async/vision/roadmap.html). Or you could read the async [project goal](https://github.com/rust-lang/rust-project-goals/issues/105) within the Rust Project.
49+
50+
Rust is an open source project. If you'd like to contribute to development of async Rust, start at the [contributing docs](https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md) in the main Rust repo.
51+
52+
53+
[^blocking]: This is actually a bad example because `println` is *blocking IO* and it is generally a bad idea to do blocking IO in async functions. We'll explain what blocking IO is in [chapter TODO]() and why you shouldn't do blocking IO in an async function in [chapter TODO]().

0 commit comments

Comments
 (0)