Skip to content

Commit 2dfdc1c

Browse files
committed
polish lib.rs examples
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent f611cec commit 2dfdc1c

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

src/lib.rs

+44-6
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 move { 1u8 };
158+
//! let b = || async move { 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

+5-3
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)