Skip to content
Mark Nadal edited this page Jun 25, 2021 · 9 revisions

A JavaScript Hacker's Cheatsheet Guide to Rust

While Mark is porting his database to Rust, all he wanted to know was how to do this javascript in Rust.

var Groceries = {
  add_milk: function(obj){
    obj.milk = "1gallon whole";
  }
}

var obj = {};
Groceries.add_milk(obj);
console.log(obj.milk);

Here's how: (try it on the online Rust playground)

use std::collections::HashMap;

type Object<'a> = HashMap<&'a str, Value<'a>>;
#[derive(Debug)]
enum Value<'a>{
    Null(Option<bool>),
    Bit(bool),
    Number(f32),
    Text(&'a str),
    Link(Object<'a>)
}

struct Groceries;
impl Groceries {
    fn add_milk(obj: &mut Object) {
        obj.insert("milk", Value::Text("1gallon whole"));
    }
}

fn main() {
    let mut obj = Object::new();
    Groceries::add_milk(&mut obj);
    println!("{:?}", obj.get("milk"));
}

This wiki is where all the GUN website documentation comes from.

You can read it here or on the website, but the website has some special features like rendering some markdown extensions to create interactive coding tutorials.

Please feel free to improve the docs itself, we need contributions!

Clone this wiki locally