Created
March 31, 2016 15:28
-
-
Save lnds/b9d910d5ae92dd2bcf8fe7710f585088 to your computer and use it in GitHub Desktop.
channels in rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let (tx, rx) = mpsc::channel(); | |
for city in cities { | |
let tx = tx.clone(); | |
let city = city.clone(); | |
thread::spawn(move || { | |
let report = api_call(&city, &api_key); | |
tx.send(report).unwrap(); | |
}); | |
} | |
let mut reports : Vec<ApiResult> = Vec::with_capacity(cities.len()); | |
for _ in cities { | |
let rep = rx.recv().unwrap(); | |
reports.push(rep); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment