Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust comparison example is unnecessarily complicated #199

Closed
gquintard opened this issue Jan 5, 2022 · 5 comments
Closed

Rust comparison example is unnecessarily complicated #199

gquintard opened this issue Jan 5, 2022 · 5 comments

Comments

@gquintard
Copy link

gquintard commented Jan 5, 2022

keeping the same dependencies and logic, the rust code boils down to

use serde::Deserialize;

const STORIES_URL: &str = "https://hacker-news.firebaseio.com/v0/topstories.json";
const ITEM_URL_BASE: &str = "https://hacker-news.firebaseio.com/v0/item";

#[derive(Deserialize)]
struct Story {
    title: String,
}

fn main() {
    let story_ids: Vec<u64> = reqwest::blocking::get(STORIES_URL).unwrap().json().unwrap();
    let mut handles = Vec::new();
    for id in &story_ids[..8] {
        let oid = id.clone();
        handles.push(std::thread::spawn(move || {
            let story_url = format!("{}/{}.json", ITEM_URL_BASE, oid);
            let story: Story = reqwest::blocking::get(&story_url).unwrap().json().unwrap();
            println!("{}", story.title);
        }));
    }
    for handle in handles {
        handle.join().unwrap();
    }
}

with Cargo.toml:

[package]
name = "hnfetch"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0", features = ["derive"] }
reqwest = { version = "0.11", features = ["json", "blocking"] }
tokio = { version = "1", features = ["full"] }

if you want, you can can go full-on with iterators and maps:

use serde::Deserialize;

const STORIES_URL: &str = "https://hacker-news.firebaseio.com/v0/topstories.json";
const ITEM_URL_BASE: &str = "https://hacker-news.firebaseio.com/v0/item";

#[derive(Deserialize)]
struct Story {
    title: String,
}

fn main() {
    let story_ids: Vec<u64> = reqwest::blocking::get(STORIES_URL).unwrap().json().unwrap();
    story_ids[..8]
        .into_iter()
        .copied()
        .map(|id| {
            std::thread::spawn(move || {
                let story_url = format!("{}/{}.json", ITEM_URL_BASE, id);
                let story: Story = reqwest::blocking::get(&story_url).unwrap().json().unwrap();
                println!("{}", story.title);
            })
        })
        .collect::<Vec<_>>()
        .into_iter()
        .for_each(|handle| handle.join().unwrap())
}

but neither case is as scary as the current code. I can offer a PR if needed

@barakplasma
Copy link

I reviewed this code as well and agree that the example given is a bit unfair to Rust. That being said, I think the point was to show how vlang makes it easier to use a lock on the cursor.

@gquintard
Copy link
Author

my issue is that while the point may be true, making it badly is counter-productive and will make newcomers familiar with rust doubt

@medvednikov
Copy link
Member

All examples use only stdlib.

I understand Rust has lots of cool crates, but it's more to showcase the languages' bulitin tools.

Otherwise we also could use an HN lib that could achieve this in one line.

@medvednikov
Copy link
Member

I've added this info to the website.

@gquintard
Copy link
Author

All examples use only stdlib.

The current example doesn't? That was the point of this issue: using the same dependencies, the current example could be a lot simpler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants