This may not be the right question to ask, but I've been wrestling with the borrow checker for a while.
I am trying to have a struct Bridge
with a property token_manager: TokenManger
that owns a tokens: HashMap<[u8; 4], Token>.
The Token
has an id: TokenId
that has a bytes: [u8; 4]
.
The only mutable element is the HashMap, and that's only mutable so I can add more tokens.
I create this with
// This parses a json file and returns a collection of Tokens
// Each Token has a TokenId, which includes a byte array
let tokens: Vec<Token> = manifests::tokens::load("path/to/tokens.json");
// The Token Manager just has a hashmap and some accessors, mutators, and adders
let mut token_manager = Manager::new();
for token in tokens {
// add() simply `self.tokens.insert(&token.id.byttes, token);`
token_manager.add(&token);
}
RfBridge {
tokens: &tokens_manager
}
The error I'm getting is:
error[E0515]: cannot return value referencing local variable `token_manager`
--> src/bridge.rs:42:9
|
42 | / RfBridge {
45 | | tokens: &token_manager
| | ------------------ `token_manager` is borrowed here
46 | | }
| |_________^ returns a value referencing data owned by the current function
I have edited the code to remove unnecessary parts, but am happy to share more. Any help would be greatly appreciated. Even just a point in the right direction.