Synchronous programming completes a called operation before continuing with subsequent work. If that operation blocks while waiting for a file or database, the current thread waits too. section 10.1 section 10.2
Two sequential reads
In src/main.rs, main() calls read_from_file1(), prints its result, then calls read_from_file2(). Each function uses std::thread::sleep to simulate a delay. With waits of four and two seconds, the program waits six seconds in total: the second call cannot begin while the first is unfinished. section 10.2
use std::thread::sleep;
use std::time::Duration;
fn main() {
println!("Hello before reading file!");
let file1_contents = read_from_file1(); #1
println!("{:?}", file1_contents);
println!("Hello after reading file1!");
let file2_contents = read_from_file2(); #2
println!("{:?}", file2_contents);
println!("Hello after reading file2!");
}
// function that simulates reading from a file
fn read_from_file1() -> String {
sleep(Duration::new(4, –));
String::from("Hello, there from file 1")
}
// function that simulates reading from a file
fn read_from_file2() -> String {
sleep(Duration::new(2, –));
String::from("Hello, there from file 2")
}
The Rust listing shows both calls in order, with a blocking sleep inside each function. section 10.2
When independent work need not wait, multithreading can run a blocking task on another operating-system thread. Async processing instead lets a runtime such as Tokio schedule another task while one waits for I/O. Neither changes what the synchronous call itself does: its caller waits for a result before proceeding. section 10.1 section 10.2





