Like in a previous question of mine, I'm refactoring a small Rust project. Also similar to that question, the compiler and I don't see eye to eye on methods and values. The error message is very different, though, and this seems to potentially involve libraries, hyper in this example.
/* extern and use directives... */
pub struct WebServer /* with stuff not relevant to the error */;
impl WebServer {
pub fn serve(&mut self) {
let server = Server::http(Ipv4Addr(127, 0, 0, 1), 80);
let mut listening = server.listen(self.return_page).unwrap();
listening.await();
}
fn return_page(&mut self, req: Request, mut res: Response) {
/* Put the page together */
}
}
When serve()
and return_page()
are just functions in main.rs
, this compiles and runs.
Tucked into a struct
as above, though, I get the following error:
.../src/main.rs:13:48: 13:59 error: attempted to take value of method `return_page` on type `&mut WebServer`
.../src/main.rs:13 let mut listening = server.listen(self.return_page).unwrap();
^~~~~~~~~~~
.../src/main.rs:13:48: 13:59 help: maybe a `()` to call it is missing? If not, try an anonymous function
.../src/main.rs:13 let mut listening = server.listen(self.return_page).unwrap();
^~~~~~~~~~~
I assume that the parser just needs a nudge in the right direction, but even with the advice I was given on the related problem, I don't see anything in the documentation suggesting a way to say, "no, I really mean the function as a parameter, just like invoked function's signature says I need." What am I missing, given that the above syntax works outside of a struct
?
A half-hearted attempt at a closure seemed to be worse and the error message still suggested calling return_page
instead of passing it, but I may well be misunderstanding the usage in this case.
Edit: To clarify the situation after working with the existing answers, the original return_page()
doesn't have a self
parameter, of course, which is itself part of the problem. But it also needs self
inside the object, because it needs access to member variables.