Deleting an entry from BTreeMap<String, ..>


#1

I’m using serde_json which converts json to BTreeMap. I would like to delete some unnecessary keys from it and convert it back to json string.

        let mut tree = message.as_object().unwrap();
        let mut del_keys = vec!["__BOOL"];
        
        for key in del_keys.iter(){
            tree.remove(&key);
        }

Error:

error: the trait `core::borrow::Borrow<&str>` is not implemented for the type `collections::string::String` [E0277]
src/main.rs:54             tree.remove(key);

PS: I’m not sure if I can ask very basic questions like this here.


#2

The code you’ve given and the error don’t match.


#3

Sorry. Please Ignore this question. I just need to do this

let mut tree = message.as_object_mut().unwrap();
        let mut del_keys = vec!["__BOOL", "__CURSOR", "_BOOT_ID"];

        for (key, value) in tree.iter(){
            println!("{:?}", key);
        }
        
        for key in del_keys.iter(){
            tree.remove(&mut key.to_string());
        }

Because

https://serde-rs.github.io/serde/serde_json/serde_json/value/enum.Value.html#method.as_object