Skip to content

Instantly share code, notes, and snippets.

@daboross
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save daboross/cb4331a56aeb5ae71d82 to your computer and use it in GitHub Desktop.

Select an option

Save daboross/cb4331a56aeb5ae71d82 to your computer and use it in GitHub Desktop.
extern crate serialize;
extern crate std;
extern crate collections;
use self::serialize::json;
use self::serialize::json::Json;
use self::std::io::File;
use self::collections::TreeMap;
pub fn load_config_map() -> Option<&TreeMap<~str, Json>> {
let config: Json = match json::from_reader(&mut File::open(&Path::new("config.json"))) {
Ok(v) => v,
Err(e) => fail!("Failed to decode config: {}", e)
};
let config_map: &TreeMap<~str, Json> = match config.as_object() {
Some(v) => v,
None => fail!("Config isn't an object!")
};
return Some(config_map);
}

I'm getting this error when compiling:

src/config.rs:16:50: 16:56 error: `config` does not live long enough
src/config.rs:16     let config_map: &TreeMap<~str, Json> = match config.as_object() {
                                                                  ^~~~~~
src/config.rs:11:58: 21:2 note: reference must be valid for the anonymous lifetime #1 defined on the block at 11:57...
src/config.rs:11 pub fn load_config_map() -> Option<&TreeMap<~str, Json>> {
src/config.rs:12     let config: Json = match json::from_reader(&mut File::open(&Path::new("config.json"))) {
src/config.rs:13         Ok(v) => v,
src/config.rs:14         Err(e) => fail!("Failed to decode config: {}", e)
src/config.rs:15     };
src/config.rs:16     let config_map: &TreeMap<~str, Json> = match config.as_object() {
                 ...
src/config.rs:11:58: 21:2 note: ...but borrowed value is only valid for the block at 11:57
src/config.rs:11 pub fn load_config_map() -> Option<&TreeMap<~str, Json>> {
src/config.rs:12     let config: Json = match json::from_reader(&mut File::open(&Path::new("config.json"))) {
src/config.rs:13         Ok(v) => v,
src/config.rs:14         Err(e) => fail!("Failed to decode config: {}", e)
src/config.rs:15     };
src/config.rs:16     let config_map: &TreeMap<~str, Json> = match config.as_object() {
                 ...
error: aborting due to previous error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment