Reversing a number

Is there a way to reverse a number instead of using 1/number, something like the below code!

fn main() {
    let v = 3.reverse();
    println!("{}", v);
}
trait Inverse {
    type Output;

    fn inverse(self) -> Self::Output;
}

impl Inverse for f32 {
    type Output = f32;

    fn inverse(self) -> Self::Output {
        1./self
    }
}

If you’re doing this for f32, recip already exists.

3 Likes

What shall I use instead of impl Inverse for f32 if I want to implement it for all types of numbers!

The num crate provides generic abstractions over numbers, for example num::Float - Rust