Casting#
as is the explicit numeric cast.
Truncating integer cast.
let n: i32 = 250;
let b: u8 = n as u8; // wraps to 250
Float to integer; truncates toward zero.
let f: f64 = 3.7;
let i: i32 = f as i32; // 3
For semantic conversions, From / Into (see
Patterns). For fallible conversions, TryFrom /
TryInto.
let s: String = "hi".to_string(); // From<&str>
let n: u32 = u32::from(7u8); // From<u8>
let big: u8 = u8::try_from(1000)?; // TryFrom<u32>
References#
Primitives for numeric types and width concerns.
Operators for
asprecedence and overflow semantics.