0Pricing
Learn Rust Coding · Aula

Tipos escalares

Inteiros, números de ponto flutuante, booleano e caractere

Tipos escalares é uma aula grátis de Learn Rust Coding no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Learn Rust Coding, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Learn Rust Coding inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

What Are Scalar Types?

A scalar type represents a single value. Rust has four primary scalar types:

  • Integers
  • Floating-point numbers
  • Booleans
  • Characters
fn main() {
    let i = 10;       // integer
    let f = 2.5;      // float
    let b = true;     // bool
    let c = 'R';      // char
    println!("{} {} {} {}", i, f, b, c);
}

Integer Types

Integers come in signed (i) and unsigned (u) variants with fixed sizes: i8, i16, i32, i64, i128, and the same for u.

The default integer type is i32.

fn main() {
    let small: i8 = -120;
    let big: u64 = 18_000_000_000;
    println!("{} {}", small, big);
}

Signed vs Unsigned

Signed integers can be negative; unsigned cannot.

  • i8 ranges from -128 to 127.
  • u8 ranges from 0 to 255.

Choose unsigned when a value can never be negative, like a count.

fn main() {
    let temperature: i32 = -15;
    let people: u32 = 250;
    println!("temp {} people {}", temperature, people);
}

Integer Literals

You can write integers in different bases and use underscores for readability.

  • Decimal: 1_000_000
  • Hex: 0xff
  • Octal: 0o77
  • Binary: 0b1010
fn main() {
    let dec = 1_000;
    let hex = 0xff;
    let bin = 0b1010;
    println!("{} {} {}", dec, hex, bin);
}

Floating-Point Types

Rust has two float types: f32 and f64. The default is f64 because it offers more precision.

Floats follow the IEEE-754 standard.

fn main() {
    let pi: f64 = 3.141592;
    let e: f32 = 2.718;
    println!("pi {} e {}", pi, e);
}

Numeric Operations

Rust supports the usual math operators: +, -, *, /, and % (remainder).

Integer division truncates toward zero.

fn main() {
    let sum = 5 + 10;
    let quotient = 7 / 2;     // 3 (integer)
    let remainder = 7 % 2;    // 1
    let float_div = 7.0 / 2.0; // 3.5
    println!("{} {} {} {}", sum, quotient, remainder, float_div);
}

The Boolean Type

The bool type has exactly two values: true and false.

Booleans are produced by comparisons and used in if conditions.

fn main() {
    let is_active = true;
    let is_bigger = 10 > 3;
    println!("{} {}", is_active, is_bigger);
}

The Character Type

A char represents a single Unicode scalar value and is written with single quotes.

A Rust char is 4 bytes and can hold far more than ASCII — emoji and accented letters included.

fn main() {
    let letter = 'A';
    let symbol = '@';
    let heart = '\u{2764}';
    println!("{} {} {}", letter, symbol, heart);
}

char vs String

Single quotes make a char; double quotes make a string slice (&str).

  • 'a' is a single character.
  • "a" is a one-character string.
fn main() {
    let c = 'x';
    let s = "x";
    println!("char {} string {}", c, s);
}

Type Conversion with as

Rust does not convert numeric types automatically. Use the as keyword for explicit casts.

Be careful: casting to a smaller type can lose data.

fn main() {
    let x: i32 = 65;
    let y: f64 = x as f64;
    let c = x as u8 as char;
    println!("{} {} {}", x, y, c);
}

Putting Scalars Together

Programs combine scalar types constantly. Here we mix integers, floats, and booleans to make a decision.

fn main() {
    let price: f64 = 19.99;
    let quantity: u32 = 3;
    let total = price * quantity as f64;
    let is_expensive = total > 50.0;
    println!("total {} expensive {}", total, is_expensive);
}

Quick Check

Test your knowledge of Rust scalar literals.

Recap

Rust's four scalar types:

  • Integers — signed (i32) and unsigned (u32), various sizes.
  • Floats — f32 and f64, default f64.
  • bool — true or false.
  • char — a single Unicode value in single quotes.

Use as for explicit numeric conversions.

fn main() {
    let count: u32 = 42;
    let ratio: f64 = count as f64 / 7.0;
    let ok: bool = ratio > 5.0;
    let grade: char = 'B';
    println!("{} {} {} {}", count, ratio, ok, grade);
}

Perguntas Frequentes

A aula “Tipos escalares” é grátis?

Sim — o texto completo de “Tipos escalares” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Learn Rust Coding, atualize para CoddyKit PRO. O curso de Learn Rust Coding inclui 4 aulas no total.

O que vou aprender em “Tipos escalares”?

Inteiros, números de ponto flutuante, booleano e caractere Você pratica Learn Rust Coding com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Learn Rust Coding?

Nenhuma experiência prévia é necessária. Learn Rust Coding no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.

Quanto tempo leva a aula “Tipos escalares”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Learn Rust Coding?

Sim. Cada aula de Learn Rust Coding inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. let e mutabilidade
  2. Tipos escalares
  3. Tipos compostos
  4. Sombreamento
← Voltar para Learn Rust Coding