Rust is a statically typed language, which means the type of every variable must be known at compile time. In many cases, Rust can infer the type for you, but sometimes you must explicitly provide it.
Data types are the foundation of any program, and understanding them is essential to writing Rust code effectively. In this tutorial, we will explore Rust’s most common basic data types:
- Integers
- Floating-point numbers
- Booleans
- Characters
- Strings
Integers #
An integer is a whole number, without any fractional component. Rust has several integer types, both signed (i) and unsigned (u), with different sizes.
Examples of integer types:
i8,i16,i32,i64,i128,isize(signed, can store negative values)u8,u16,u32,u64,u128,usize(unsigned, only positive values)
By default, Rust uses i32 when you don’t specify a type.
fn main() {
let a: i32 = -42; // signed 32-bit integer
let b: u64 = 100; // unsigned 64-bit integer
let c = 10; // inferred as i32
println!("a = {}, b = {}, c = {}", a, b, c);
}
Code language: Rust (rust)Integer Literals #
You can write integers in different number systems:
- Decimal:
98_222(underscores for readability) - Hex:
0xff - Octal:
0o77 - Binary:
0b1111_0000 - Byte (only for
u8):b'A'
Floating-Point Numbers #
Floating-point numbers represent real numbers with fractional parts. Rust has two main types:
f32(32-bit floating point)f64(64-bit floating point, default)
fn main() {
let x = 2.0; // f64 by default
let y: f32 = 3.5; // explicitly f32
println!("x = {}, y = {}", x, y);
}
Code language: Rust (rust)Booleans #
Booleans are simple data types with two possible values: true or false. They are represented by the bool type.
fn main() {
let is_rust_fun: bool = true;
let is_boring = false; // inferred as bool
println!("Rust fun? {}, Rust boring? {}", is_rust_fun, is_boring);
}
Code language: Rust (rust)Booleans are often used in conditional statements:
fn main() {
let condition = true;
if condition {
println!("Condition is true!");
} else {
println!("Condition is false!");
}
}
Code language: Rust (rust)Characters #
Rust’s char type represents a single Unicode scalar value, not just ASCII. This means it can store more than just English letters—it supports emojis and characters from many languages.
fn main() {
let c = 'z';
let heart = '♥';
let smile = '😊';
println!("Chars: {}, {}, {}", c, heart, smile);
}
Code language: Rust (rust)Note: A char is written in single quotes ('), not double quotes.
Strings #
Rust has two common string types:
&str(string slice)String(a growable, heap-allocated string)
String Slice (&str) #
A string slice is a reference to a sequence of UTF-8 characters. Most of the time, string literals are of type &str.
fn main() {
let greeting: &str = "Hello, world!";
println!("{}", greeting);
}
Code language: Rust (rust)String (String) #
A String is an owned, growable string stored on the heap. You can create and modify it.
fn main() {
let mut s = String::from("Hello");
s.push_str(", Rust!"); // add more text
println!("{}", s);
}
Code language: Rust (rust)Putting It All Together #
Here’s an example combining all these types:
fn main() {
let age: u32 = 25; // integer
let height = 5.9; // float (f64)
let is_student = true; // boolean
let initial: char = 'D'; // character
let name = String::from("Anthony"); // String
println!("Name: {}, Initial: {}", name, initial);
println!("Age: {}, Height: {}", age, height);
println!("Is student? {}", is_student);
}
Code language: Rust (rust)Key Takeaways #
- Rust requires knowing the type of every variable at compile time.
- Integers can be signed (
i32) or unsigned (u32), with different sizes. - Floating-point numbers (
f32,f64) represent real numbers. - Booleans are
trueorfalseand often used in conditionals. - Characters (
char) represent Unicode, so they can store letters, symbols, and emojis. - Strings come in two main types:
&str(immutable slice) andString(growable heap-allocated).