Rust cookbook

Some findings and notes about using rust on some real-world stuff

Handling JSON

A small example how to dump your struct to a file and read it again.

extern crate serde_derive;
extern crate serde;
extern crate serde_json;
use std::io::BufWriter;
use std::io::BufReader;
use std::fs::File;

#[derive(Serialize, Deserialize, Debug)]
enum CarClass {

#[derive(Serialize, Deserialize, Debug)]
struct Car {
    maxspeed: f64,
    weight: f64,
    class: CarClass

fn main() {
    let new_car = Car {
        maxspeed: 120.0,
        weight: 4000.0,
        class: CarClass::Offroad
    // write out the file
    let writer = BufWriter::new(File::create("car.json").unwrap());
    serde_json::to_writer_pretty(writer, &new_car).unwrap();

    // read it back again
    let reader = BufReader::new(File::open("car.json").unwrap());
    let loaded_car: Car = serde_json::from_reader(reader).unwrap();
    println!("{:?}", loaded_car);


serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"