Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c9a2e74

Browse files
Merge pull request #523 from async-rs/update-lib-example
polish lib.rs examples
2 parents 02e1d7e + cffacf7 commit c9a2e74

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

‎.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
uses: actions-rs/cargo@v1
6363
with:
6464
command: test
65-
args: --all --features unstable
65+
args: --all --features unstable attributes
6666

6767
check_fmt_and_docs:
6868
name: Checking fmt and docs

‎README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,28 @@ syntax.
7474

7575
## Examples
7676

77+
All examples require the [`"attributes"` feature] to be enabled. This feature
78+
is not enabled by default because it significantly impacts compile times. See
79+
[`task::block_on`] for an alternative way to start executing tasks.
80+
7781
```rust
78-
use async_std::task;
82+
async fn say_hello() {
83+
println!("Hello, world!");
84+
}
7985

80-
fn main() {
81-
task::block_on(async {
82-
println!("Hello, world!");
83-
})
86+
#[async_std::main]
87+
async fn main() {
88+
say_hello().await;
8489
}
8590
```
8691

8792
More examples, including networking and file access, can be found in our
88-
[`examples`] directory.
93+
[`examples`] directory and in our [documentation].
8994

9095
[`examples`]: https://github.com/async-rs/async-std/tree/master/examples
96+
[documentation]: https://docs.rs/async-std#examples
97+
[`task::block_on`]: task/fn.block_on.html
98+
[`"attributes"` feature]: https://docs.rs/async-std/#features
9199

92100
## Philosophy
93101

‎src/lib.rs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,55 @@
131131
//!
132132
//! # Examples
133133
//!
134-
//! Spawn a task and block the current thread on its result:
134+
//! All examples require the [`"attributes"` feature](#features) to be enabled.
135+
//! This feature is not enabled by default because it significantly impacts
136+
//! compile times. See [`task::block_on`] for an alternative way to start
137+
//! executing tasks.
135138
//!
139+
//! Call an async function from the main function:
140+
//!
141+
//! ```
142+
//! async fn say_hello() {
143+
//! println!("Hello, world!");
144+
//! }
145+
//!
146+
//! #[async_std::main]
147+
//! async fn main() {
148+
//! say_hello().await;
149+
//! }
150+
//! ```
151+
//!
152+
//! Await two futures concurrently, and return a tuple of their output:
153+
//!
154+
//! ```
155+
//! #[async_std::main]
156+
//! async fn main() {
157+
//! let a = async { 1u8 };
158+
//! let b = async { 2u8 };
159+
//! assert_eq!(a.join(b).await, (1u8, 2u8))
160+
//! }
136161
//! ```
137-
//! use async_std::task;
138162
//!
139-
//! fn main() {
140-
//! task::block_on(async {
141-
//! println!("Hello, world!");
142-
//! })
163+
//! Create a UDP server that echoes back each received message to the sender:
164+
//!
165+
//! ```no_run
166+
//! use async_std::net::UdpSocket;
167+
//!
168+
//! #[async_std::main]
169+
//! async fn main() -> std::io::Result<()> {
170+
//! let mut socket = UdpSocket::bind("127.0.0.1:8080")?;
171+
//! println!("Listening on {}", socket.local_addr()?);
172+
//!
173+
//! let mut buf = vec![0u8; 1024];
174+
//!
175+
//! loop {
176+
//! let (recv, peer) = socket.recv_from(&mut buf).await?;
177+
//! let sent = socket.send_to(&buf[..recv], &peer).await?;
178+
//! println!("Sent {} out of {} bytes to {}", sent, recv, peer);
179+
//! }
143180
//! }
144181
//! ```
182+
//! [`task::block_on`]: task/fn.block_on.html
145183
//!
146184
//! # Features
147185
//!

‎src/task/block_on.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ use crate::task::{Context, Poll, Task, Waker};
2828
/// ```no_run
2929
/// use async_std::task;
3030
///
31-
/// task::block_on(async {
32-
/// println!("Hello, world!");
33-
/// })
31+
/// fn main() {
32+
/// task::block_on(async {
33+
/// println!("Hello, world!");
34+
/// })
35+
/// }
3436
/// ```
3537
pub fn block_on<F, T>(future: F) -> T
3638
where

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /