Skip to content

Instantly share code, notes, and snippets.

@lnds
Created March 31, 2016 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lnds/b9d910d5ae92dd2bcf8fe7710f585088 to your computer and use it in GitHub Desktop.
Save lnds/b9d910d5ae92dd2bcf8fe7710f585088 to your computer and use it in GitHub Desktop.
channels in rust
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