Hi there!
I am really new to Rust, but I would love to use it as a web server.
I encounter issues regarding mutable reference borrowing (using Iron, but my issue is general).
The action called by Iron after routing is as follows:
pub fn hello(req: &mut Request) -> IronResult<Response>
I would like to call a centralized code in an struct of mine, that checks the request headers and returns a response in case of error (and I won't duplicate this code in each and every action I will have to write)
This is my method:
pub fn init(&mut self, req: &mut Request)
I call it (inside "hello()") like this:
req.extensions.get_mut::<App>().unwrap().init(req);
But Rust does not like it and it tells me:
error: cannot borrow `*req` as mutable more than once at a time
note: previous borrow of `req.extensions` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `req.extensions` until the borrow ends
note: previous borrow ends here (end of function)
How am I supposed to do this? I have spent hours reading the doc and browsing the web, but to no avail. Can't I just call a function and give it my reference? I thought I could, since the init() method ends and returns the (temporary) ownership of the reference back to the hello() method... It means that only one method can actually use that reference at a given time. Right?
Any tips to help me better understand what I am doing wrong and the proper way to do it, would be much appreciated!
Sincerely,