0Pricing
Learn Rust Coding · Lección

Tipos de datos primitivos y operadores

Explore los tipos de datos primitivos integrados de Rust, como enteros, números de coma flotante, booleanos y caracteres, junto con los operadores habituales.

Tipos de datos primitivos y operadores es una lección gratuita de Learn Rust Coding en CoddyKit. Esta es la lección 2 de 3. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Learn Rust Coding, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Learn Rust Coding incluye 3 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

Intro to Primitive Data Types

Welcome to a foundational lesson in Rust! Today, we'll explore primitive data types and basic operators.

Primitive types are the most fundamental building blocks for storing data. They represent simple values like numbers, true/false, or single characters.

Understanding these types is crucial for writing efficient and correct Rust code.

Rust's Integer Types

Rust provides various integer types to fit different needs. They come in two main flavors:

  • Signed Integers (i prefix): Can store both positive and negative numbers (e.g., i8, i32, i64).
  • Unsigned Integers (u prefix): Can only store positive numbers or zero (e.g., u8, u32, u64).

The number indicates the bits of storage, e.g., i32 is a 32-bit signed integer. isize and usize are pointer-sized integers, adapting to your system's architecture (32-bit or 64-bit).

Declaring Integer Variables

Let's see how to declare and use integer types in Rust. Notice how we can explicitly add a type annotation or let Rust infer it.

Try running this example:

fn main() {
  let signed_val: i32 = -100;
  let unsigned_val: u32 = 250;
  let inferred_int = 500; // Rust infers i32 by default
  let big_unsigned: u64 = 1_000_000_000;

  println!("Signed: {}", signed_val);
  println!("Unsigned: {}", unsigned_val);
  println!("Inferred: {}", inferred_int);
  println!("Big Unsigned: {}", big_unsigned);
}

Floating-Point Numbers

For numbers with decimal points, Rust offers floating-point types:

  • f32: Single-precision float (32-bit).
  • f64: Double-precision float (64-bit). This is the default floating-point type in Rust.

f64 is generally preferred for most applications due to its higher precision, unless you have specific memory or performance constraints.

Declaring Float Variables

Here's how to declare floating-point numbers. Notice that Rust defaults to f64 if you don't specify a type.

Try running this example:

fn main() {
  let pi: f32 = 3.14159;
  let e = 2.718281828; // Inferred as f64
  let temperature: f64 = 98.6;

  println!("Pi (f32): {}", pi);
  println!("E (f64 inferred): {}", e);
  println!("Temperature (f64): {}", temperature);
}

Booleans and Characters

Beyond numbers, Rust has types for logical values and single letters:

  • bool: Represents a truth value, either true or false.
  • char: Represents a single Unicode scalar value. This means a char can be more than just ASCII – it can be any letter, emoji, or symbol.

A char uses 4 bytes of memory, ensuring it can store any Unicode character.

Bools and Chars in Action

Let's declare some boolean and character variables. Notice the single quotes for char values.

Try running this example:

fn main() {
  let is_rust_fun: bool = true;
  let has_errors = false; // Inferred as bool
  let first_letter: char = 'R';
  let emoji_char: char = '🚀';

  println!("Is Rust fun? {}", is_rust_fun);
  println!("Has errors? {}", has_errors);
  println!("First letter: {}", first_letter);
  println!("Emoji: {}", emoji_char);
}

Basic Arithmetic Operators

Just like in math, Rust uses operators to perform calculations. Here are the common arithmetic operators:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Remainder (Modulo)

Remember that integer division (e.g., 10 / 3) will truncate the decimal part, resulting in an integer (3, not 3.33).

Using Arithmetic Operators

Let's put these operators into practice with some numbers. Pay attention to how integer division behaves!

Try running this example:

fn main() {
  let a = 15;
  let b = 4;

  println!("Addition: 15 + 4 = {}", a + b);
  println!("Subtraction: 15 - 4 = {}", a - b);
  println!("Multiplication: 15 * 4 = {}", a * b);
  println!("Integer Division: 15 / 4 = {}", a / b);
  println!("Remainder: 15 % 4 = {}", a % b);

  let x = 15.0;
  let y = 4.0;
  println!("Float Division: 15.0 / 4.0 = {}", x / y);
}

Primitive Types Quiz

Test your understanding of Rust's primitive data types and basic operators.

Recap: Primitives & Operators

Great job! You've learned about Rust's fundamental data types:

  • Integers: i8, u8, i32, u32, etc., for whole numbers.
  • Floats: f32 and f64 for decimal numbers.
  • Booleans: bool for true or false.
  • Characters: char for single Unicode values.

You also explored basic arithmetic operators like +, -, *, /, and %. These are the building blocks for more complex logic in your programs.

Preguntas frecuentes

¿La lección «Tipos de datos primitivos y operadores» es gratis?

Sí — el texto completo de «Tipos de datos primitivos y operadores» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Learn Rust Coding, actualiza a CoddyKit PRO. El curso de Learn Rust Coding incluye 3 lecciones en total.

¿Qué aprenderé en «Tipos de datos primitivos y operadores»?

Explore los tipos de datos primitivos integrados de Rust, como enteros, números de coma flotante, booleanos y caracteres, junto con los operadores habituales. Practicas Learn Rust Coding con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Learn Rust Coding?

No se requiere experiencia previa. Learn Rust Coding en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 3.

¿Cuánto tiempo toma la lección «Tipos de datos primitivos y operadores»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Learn Rust Coding?

Sí. Cada lección de Learn Rust Coding incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Variables, mutabilidad y sombreado
  2. Tipos de datos primitivos y operadores
  3. Funciones y flujo de control
← Volver a Learn Rust Coding