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 Data Types


Share

Data Types

Unlike many other programming languages, variables in Rust do not need to be declared with a specified type (like "String" for text or "Int" for numbers, if you are familiar with those from C or Java).

In Rust, the type of a variable is decided by the value you give it. Rust looks at the value and automatically chooses the right type:

Example

let my_num = 5;         // integer
let my_double = 5.99;   // float
let my_letter = 'D';    // character
let my_bool = true;     // boolean
let my_text = "Hello";  // string
Try it Yourself »

However, it is possible to explicitly tell Rust what type a value should be:

Example

let my_num: i32 = 5;          // integer
let my_double: f64 = 5.99;    // float
let my_letter: char = 'D';    // character
let my_bool: bool = true;     // boolean
let my_text: &str = "Hello";  // string
Try it Yourself »

You will learn more about when you need to specify the type later in this tutorial. Either way, it is good to understand what the different types mean.

Basic data types in Rust are divided into different groups:

  • Numbers - Whole numbers and decimal numbers (i32, u32, f64)
  • Characters - Single letters or symbols (char)
  • Strings - Text, a sequence of characters (&str)
  • Booleans - True or false values (bool)

Numbers

Number types are divided into two groups: integer types and floating point types.

Integers (i32 and u32)

Integer types are used to store whole numbers without decimals.

Two common integer types are i32 and u32:

  • i32 can store positive and negative whole numbers
  • u32 can only store zero or positive whole numbers

Example

let temperature: i32 = -5;
let age: u32 = 25;

println!("Temperature: {}", temperature);
println!("Age: {}", age);
Try it Yourself »

The u in u32 means unsigned, which means the value cannot be negative. This makes u32 useful for values such as age, counts, and sizes.

Floating Point (f64)

The f64 type is used to store numbers containing one or more decimals:

Example

let price: f64 = 19.99;
println!("Price is: ${}", price);
Try it Yourself »

Other Number Types

Rust has more number types than i32, u32 and f64. The number in the name is the size of the type in bits. A bigger type can store bigger numbers:

TypeStores
i8Whole numbers from -128 to 127
u8Whole numbers from 0 to 255
i32Whole numbers from -2147483648 to 2147483647
u32Whole numbers from 0 to 4294967295
i64Whole numbers from -9223372036854775808 to 9223372036854775807
u64Whole numbers from 0 to 18446744073709551615
f32Decimal numbers, with about 7 correct digits
f64Decimal numbers, with about 15 correct digits

There are also i16, u16, i128 and u128, and usize, which Rust uses for sizes and index numbers.

Example

let small: u8 = 255;
let big: i64 = 9000000000;
let precise: f32 = 3.5;

println!("small: {}", small);
println!("big: {}", big);
println!("precise: {}", precise);
Try it Yourself »

Note: If you do not write a type, Rust uses i32 for whole numbers and f64 for decimal numbers. A value that does not fit in its type, like let x: u8 = 256;, gives a compile error.



Characters (char)

The char type is used to store a single character. A char value must be surrounded by single quotes, like 'A' or 'c':

Example

let my_grade: char = 'B';
println!("{}", my_grade);
Try it Yourself »

Strings (&str)

The &str type is used to store a sequence of characters (text). String values must be surrounded by double quotes:

Example

let name: &str = "John";
println!("Hello, {}!", name);
Try it Yourself »

Booleans (bool)

The bool type can only take the values true or false:

Example

let is_logged_in: bool = true;
println!("User logged in? {}", is_logged_in);
Try it Yourself »

Combining Data Types

You can mix different types in the same program:

Example

let name = "John";
let age = 28;
let is_admin = false;
println!("Name: {}", name);
println!("Age: {}", age);
println!("Admin: {}", is_admin);
Try it Yourself »



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.