How do I extract a plain int from a `std::num::Wrapping`?

I want to use a std::num::Wrapping to do some wrapping u16 arithmetic, and then I want to extract the value and promote it to a u32. I can’t work out how to get a plain integer back out of the Wrapping.

I’m trying something like this:

use std::num::Wrapping;

fn a() {
    let x = Wrapping(42u16);
    let y: u32 = x.into();
}

Wrapping is declared as pub struct Wrapping<T>(pub T); - a tuple struct with a single public field, so you can use x.0 to get the field:

use std::num::Wrapping;

fn main() {
    let x = Wrapping(42_u16);
    let y: u32 = x.0.into();
    println!("y = {}", y);
}

Playground

UPD: you can read more about tuple structs in the book.

1 Like

Oh right, that is even suggested in the doc example, but could be more obvious. Thanks!

Do you think they’d accept a patch to make this explicit?

1 Like

Yes, more docs are always welcome!

2 Likes
1 Like