Skip to content

Instantly share code, notes, and snippets.

@scurest
Created February 27, 2019 10:23
Show Gist options
  • Save scurest/ef98b57a3c7d45aeb1ce5e8d20f09a71 to your computer and use it in GitHub Desktop.
Save scurest/ef98b57a3c7d45aeb1ce5e8d20f09a71 to your computer and use it in GitHub Desktop.
V language comparison task in Rust
extern crate crossbeam;
extern crate reqwest;
extern crate serde;
extern crate serde_json;
use crossbeam::atomic::AtomicCell;
use crossbeam::thread::scope;
use reqwest::Client;
use serde::Deserialize;
#[derive(Deserialize)]
struct Story {
title: String,
}
fn main() {
let client = Client::new();
let body = client.get("https://hacker-news.firebaseio.com/v0/topstories.json")
.send().unwrap()
.text().unwrap();
let ids: Vec<i32> = serde_json::from_str(&body).unwrap();
let cursor = AtomicCell::new(0usize);
scope(|s| {
for _ in 0..8 {
s.spawn(|_| {
while let Some(id) = ids.get(cursor.fetch_add(1)) {
let url = format!("https://hacker-news.firebaseio.com/v0/item/{}.json", id);
let body = client.get(&url)
.send().unwrap()
.text().unwrap();
let story: Story = serde_json::from_str(&body).unwrap();
println!("{}", story.title);
}
});
}
}).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment