-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Rust
Mark Nadal edited this page Jun 25, 2021
·
9 revisions
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"));
}