Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Rust Type Casting


Share

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

FromToHowExample
i32f64astotal as f64
f64i32as (the decimals are cut off)9.99 as i32 is 9
&stri32parse()let n: i32 = "42".parse().unwrap();
i32Stringto_string()42.to_string()


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.