vlang / website

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rust comparison example is unnecessarily complicated

gquintard opened this issue · comments

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

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.

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

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.

I've added this info to the website.

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.