Rust Type Casting
Type Casting
In the Data Types chapter, you learned that every value in Rust has a type, like i32 for whole numbers and f64 for decimal numbers.
Sometimes you have a value of one type, but you need it as another type. For example, the points in a game are whole numbers (i32), but the average points can have decimals.
Changing a value from one type to another is called type casting (or type conversion).
Rust Does Not Convert Automatically
Many languages change the type for you when you mix numbers. Rust does not. If you add an i32 and an f64, you get an error:
Example
let a: i32 = 10;
let b: f64 = 2.5;
let c = a + b; // Error
println!("{}", c);
Try it Yourself »
This is on purpose. Rust wants you to decide how a value is converted, so nothing changes without you knowing it.
The as Keyword
To convert one number type to another, write as followed by the new type:
Example
let total: i32 = 7;
let count: i32 = 2;
println!("Without casting: {}", total / count);
let average = total as f64 / count as f64;
println!("With casting: {}", average);
Try it Yourself »
When you divide two integers, Rust drops the decimals, so 7 / 2 is 3. After casting both values to f64, the result is 3.5.
Decimal Numbers to Integers
When you cast a decimal number to an integer, Rust cuts off the decimals. It does not round the number:
Example
let price: f64 = 9.99;
let whole = price as i32;
println!("{}", whole);
Try it Yourself »
Tip: To round to the nearest whole number first, use round(): price.round() as i32 gives 10. You will learn more about it in the Math chapter.
Between Integer Types
A u8 is a small unsigned integer type. It can only hold whole numbers from 0 to 255.
Casting to a type that can hold every value of the original type, like u8 to i32, is always safe. But be careful when you cast to a smaller type. If the number does not fit, you get a different value:
Example
let small: u8 = 200;
let big = small as i32;
println!("{}", big);
let large: i32 = 300;
let tiny = large as u8;
println!("{}", tiny);
Try it Yourself »
Here, 300 does not fit in a u8, so the result is 44. Only cast to a smaller type when you know that the value fits.
Text to Numbers
Text from a user or a file is a string, even when it contains a number. To turn a string into a number, use the parse() method (a function that belongs to a value), and write the type you want after the variable name:
Example
let text = "42";
let number: i32 = text.parse().unwrap();
println!("{}", number + 8);
Try it Yourself »
The text "42" becomes the number 42, so you can do math with it.
Not all text is a number, so parse() can fail. That is why it gives back a result that you must unpack with unwrap(). If the text is not a number, unwrap() stops the program with an error:
Example
let text = "hello";
let number: i32 = text.parse().unwrap();
println!("{}", number);
Try it Yourself »
You will learn how to handle this kind of error without stopping the program in the Error Handling chapter.
Numbers to Text
To go the other way, use to_string(). It turns a number into a String (a text type you will learn about in the Strings chapter), so you can use methods like len() on it:
Example
let year = 2025;
let text = year.to_string();
println!("{} has {} digits", text, text.len());
Try it Yourself »
Summary
| From | To | How | Example |
|---|---|---|---|
i32 | f64 | as | total as f64 |
f64 | i32 | as (the decimals are cut off) | 9.99 as i32 is 9 |
&str | i32 | parse() | let n: i32 = "42".parse().unwrap(); |
i32 | String | to_string() | 42.to_string() |