Rust for Python Developers - Unsigned, Signed Integers and Casting
Shin_O
25.6K views
This story was originally published on Medium
Follow me:
Casting in Rust
Casting means changing the data type of a piece of data from one type to another.
as
keyword turns primitive types into other primitive types. We can use as
keyword to solve the code in the introduction.
1
2
3
4
5
6
fn main() {
let a: i16 = 2;
let b: u16 = 4;
println!("{}", a+b as i16);
}
When you cast from a small length to a larger length, for example from 8-bit to 16-bit, there won’t be any problem but when you cast down, you may have a problem.
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.