Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do number literals with a suffix, like 0u8, mean in Rust?

Tags:

literals

rust

I'm reading through The Rust Programming Language and have encountered this notation: 0u8.

let some_u8_value = 0u8;
match some_u8_value {
    1 => println!("one"),
    3 => println!("three"),
    5 => println!("five"),
    7 => println!("seven"),
    _ => (),
}

After searching the web, I've found lots of examples of this notation being used (0b01001100u8, 0x82u8, 200u8), but what exactly does this notation mean?

like image 709
Jack Steam Avatar asked Nov 02 '18 14:11

Jack Steam


1 Answers

Suffixed Literals

After searching, I've found this explanation in the same book:

... all number literals except the byte literal allow a type suffix, such as 57u8...

So 0u8 is the number 0 as an unsigned 8-bit integer.

These are referred to as "suffixed literals" and are discussed at length in Rust By Example.

like image 87
Jack Steam Avatar answered Nov 01 '22 12:11

Jack Steam