2

So let's say I want to replace {"foo": 13} with {"foo": 14}. Attempt:

extern crate rustc_serialize;
use rustc_serialize::json::Json;

fn main() {
    let data = Json::from_str("{\"foo\": 13, \"bar\": \"baz\"}").unwrap();
    let mut obj = data.as_object().unwrap();
    let somenum: u64 = 14;
    obj.insert(String::from_str("foo"), Json::U64(somenum));

    for (key, value) in obj.iter() {
        println!("{}: {}", key, match *value {
            Json::U64(v) => format!("{} (u64)", v),
            Json::String(ref v) => format!("{} (string)", v),
            _ => format!("other")
        });
    }
}

Error:

src/main.rs:8:5: 8:8 error: cannot borrow immutable borrowed content `*obj` \
                            as mutable
src/main.rs:8     obj.insert(String::from_str("foo"), Json::U64(somenum));
                  ^~~
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
A T
  • 13,008
  • 21
  • 97
  • 158

1 Answers1

3

as_object returns Option<&Object>—an immutable reference.

To get a mutable reference (Option<&mut Object>), you must use as_object_mut.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215