News for hackers. Stuff that matters.
Re:What If C++ Abandoned Backward Compatibility? by steveklabnik on Saturday March 28, 2020 @03:05PM (#22713501)
For fun, I whipped this up in Rust. I decided to go with an explicit struct to serialize it into, because it makes the error handling easier, and is a bit more idiomatic. I kept unwrap because it's similar to the python semantic of throwing an exception. It's pretty close though!

    use chrono::NaiveDateTime;
    use serde::*;
    use serde_json;
    
    #[derive(Deserialize)]
    struct Person {
        name: String,
        age: u32,
        hired: String,
        emails: Vec<String>,
    }
    
    fn main() {
        let data = std::fs::read_to_string("agenda.json").unwrap();
        let mut people: Vec<Person> = serde_json::from_str(data).unwrap();
    
        people.sort_by(|a, b| b.name.cmp(&a.name));
    
        for person in &people {
            let datetime = NaiveDateTime::parse_from_str(&person.hired, "%Y-%m-%d %H:%M:%S").unwrap();
            println!(
                "{} ({}) - {}",
                person.name,
                person.age,
                datetime.format("%d/%m/%y")
            );
    
            for email in &person.emails {
                println!(" - {}", email);
            }
        }
    }
Re:What If C++ Abandoned Backward Compatibility? by steveklabnik on Saturday March 28, 2020 @08:02PM (#22715254)
Apparently you can deserialize the date directly with serde and chrono too! So this could be even shorter.
Re:What If C++ Abandoned Backward Compatibility? by BiteCode_dev on Saturday March 28, 2020 @03:28PM (#22713703)
And Rust is very, very expressive and concise for a bare metal language. Serde is a beautiful thing.