DEV Community

Discussion on: A Web App in Rust - 06 Registering a User

Collapse
 
krowemoh profile image
Nivethan • Edited

Ah! Thank you that makes sense.

& to me implies address and * means get the value at the address, would that be a valid way of thinking that data's address contains the NewUser struct? This would mean web::Form is really the address of some struct T.

Or does Deref mean something else in this context.

Collapse
 
romanlevin profile image
Roman Levin

web::Form wraps NewUser, but it isn't a pointer to a NewUser.

Deref for web::Form is very simple:

impl<T> ops::Deref for Form<T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.0
    }
}
Enter fullscreen mode Exit fullscreen mode

The additional & is necessary, because as rustc tells us, it would mean moving the NewUser out, but that's impossible because data still owns it, so borrowing (&) the dereferenced NewUser allows us to send it to values without violating ownership rules.

An alternative would be to do values(data.into_inner()) which moves the NewUser out of the web::Form by consuming it.

By the way, Nivethan, this is a wonderful series. Thank you so much for the effort you put in!