Is there a way to borrow as mutable all the way down the chain and return ownership to config? The memory operations here seem really excessive to update a single variable.
The code is working as expected but coming from high level languages it seems a little "hacky". Am I on the right track? The documentation is a little sparse on this library.
// Read Config File let mut file = File::open("config.yaml").unwrap(); let mut contents = String::new(); file.read_to_string(&mut contents); // Get Mutable 'endpoints' let mut docs = YamlLoader::load_from_str(&contents).unwrap(); let mut config = docs[0].as_hash().unwrap().clone(); let mut endpoints = config[&Yaml::from_str("endpoints")].as_vec().unwrap().clone(); // Find the correct 'endpoint' for (i, endpoint) in endpoints.clone().iter().enumerate() { let mut endpoint = endpoint.as_hash().unwrap().clone(); if endpoint[&Yaml::from_str("name")] == Yaml::from_str("Current Name") { // Found the endpoint, let's update it's name endpoint.insert(Yaml::from_str("name"), Yaml::from_str("New Name")); endpoints.insert(i, Yaml::Hash(endpoint)); config.insert(Yaml::from_str("endpoints"), Yaml::Array(endpoints.clone())); } }
as_muton the options I guess. Doubtful it'll work the whole way though.as_mut. However i kept running into this error:temporary value dropped while borrowed, consider using a 'let' binding to create a longer lived valueThis does not work:docs[0].as_hash().as_mut().unwrap();Am I usingas_mutincorrectly? I thinkunwrapmay be to blame here?unwrapwould be causing the errors. But I'm not sure what to do here... The other possibilty would be to use anif letor amatchto take out the values?into_hash(), but it looks like this attempts to move it.match. Using this in combination withborrow_mutseems to be doing exactly what I was looking for. From what I can tell this should avoid duplicating any data in memory now correct?