AnyCompiler

Basic Types

Rust's core scalar types are integers (i32, u32, i64, and others sized by bit-width and signedness), floating-point numbers (f64 by default, f32 available), bool, and char. Every variable has a type, known at compile time, whether you write it explicitly or not.

fn main() {
    let age: i32 = 30;
    let price: f64 = 19.99;
    let is_student: bool = false;
    let grade: char = 'A';

    println!("{} {} {} {}", age, price, is_student, grade);
}
Output
30 19.99 false A

Type inference

You rarely need to annotate every variable — Rust infers a type from the value and how it's used, and still enforces it exactly as if you'd written the annotation yourself. count below is inferred as i32 (Rust's default integer type) and pi as f64 (its default float type).

fn main() {
    let count = 42; // inferred as i32
    let pi = 3.14159; // inferred as f64
    println!("{} {}", count, pi);
}
Output
42 3.14159

Next: ownership — the rule that makes Rust's memory safety possible, and has no real equivalent in Python, JavaScript, or TypeScript.

Practice exercise

Declare a variable temperature: f64 = 72.5 and a variable is_sunny: bool = true. Print them together with println!("{} {}", temperature, is_sunny).