src/main.rs
async fn greet(name: &str) {
println!("Hello, {}!", name);
}
async fn add(a: i32, b: i32) -> i32 {
a + b
}
async fn fetch_message(id: u32) -> String {
// Simulate I/O with a short delay instead of real network I/O
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
format!("message #{}", id)
}
#[tokio::main]
async fn main() {
println!("=== async/await basics ===\n");
// Await async functions with .await
greet("Tokio").await;
greet("Rust").await;
let sum = add(10, 32).await;
println!("10 + 32 = {}", sum);
// Run multiple async calls sequentially
println!("\n-- sequential await --");
for id in 1..=6 {
let msg = fetch_message(id).await;
println!(" {}", msg);
}
println!("\nasync fn returns a Future.");
println!("The Future does not run until you .await it.");
}
Rust
볡μ¬
Cargo.toml
[package]
name = "tokio_example"
version = "0.1.0"
edition = "2021"
[dependencies]
bytes = "1"
futures-util = { version = "0.3", default-features = false, features = ["sink"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread", "time", "sync", "net", "io-util", "fs", "process", "signal"] }
tokio-util = { version = "0.7", features = ["codec", "rt"] }
TOML
볡μ¬
Output
async@awaits-MacBook-Air 260629_1608_rs % cargo run
Compiling tokio_example v0.1.0 (/Users/async/glory/Study/260629_1608_rs)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.16s
Running `target/debug/tokio_example`
=== async/await basics ===
Hello, Tokio!
Hello, Rust!
10 + 32 = 42
-- sequential await --
message #1
message #2
message #3
message #4
message #5
message #6
async fn returns a Future.
The Future does not run until you .await it.
async@awaits
TOML
볡μ¬
μλ
νμΈμ
β’
κ΄λ ¨ κΈ°μ λ¬Έμμ R&D 곡λ μ°κ΅¬ μ¬μ
κ΄λ ¨ λ¬Έμλ βglory@keti.re.krβλ‘ μ°λ½ λΆνλ립λλ€.
Hello 
β’
For technical and business inquiries, please contact me at βglory@keti.re.krβ




