02_variables_data_types

πŸ¦€ 30 Days Of Rust: Day 2 - Variables and Data Types πŸš€

LinkedIn Follow me on GitHub

Author: Het Patel

October, 2024

<< Day 1 | Day 3 >>

30DaysOfRust

πŸ“˜ Day 2 - Variables and Data Types

πŸ‘‹ Welcome

Welcome to Day 2 of your Rust journey! πŸŽ‰ Today, we’ll dive into variables and data types in Rust, which are fundamental concepts that will help you write efficient and safe code.

In Rust, understanding how to declare and use variables, along with knowing the different data types, is essential for building robust applications. Let's get started! πŸš€

Feel free to join the 30 Days of Rust community on Discord, where you can interact with others, ask questions, and share your progress!

πŸ” Overview

In Rust, variables are used to store data, and understanding data types is crucial for effective programming. We will cover:

  • How to declare and use variables

  • The difference between mutable and immutable variables

  • Various data types available in Rust, including scalar and compound types

πŸ›  Environment Setup

Ensure that you have your Rust environment set up correctly from Day 1. If you haven’t installed Rust yet, please refer to the setup instructions from Day 1.

πŸ“– Variables in Rust

πŸ“¦ Declare a Variable

To create a variable in Rust, you utilize the let keyword. This allows you to assign a value to the variable, making it accessible for use in your program.

Here’s how it works:

In this example, Rust infers the data type of each variable based on the assigned value. For instance, repository_nickname is recognized as a string, rating_float as a float, etc.

The println! macro used here takes two arguments:

  1. A unique syntax {} which acts as a placeholder.

  2. The name of the variable or constant to replace the placeholder with its value.

When this code runs, the output will be:

πŸ”„ Variables and Mutability

In Rust, a variable is a binding to a value. By default, variables are immutable, meaning you cannot change their value once they are set. However, you can make them mutable if needed.

πŸ“¦ Variable Binding

In Rust, you create a variable using the let keyword. Here's an example:

πŸ”’ Mutable vs Immutable Variables

By default, variables are immutable in Rust. If you want to create a mutable variable, use the mut keyword:

Example:1

Output:

This shows that we can modify the value of mutable_variable after its initial declaration.

Example:2

Example:3

You declare a variable using the let keyword:

To make a variable mutable, use the mut keyword:

πŸ“š Data Types

Rust has several built-in data types, including:

  • Scalar types: represent a single value (e.g., integers, floating-point numbers, booleans, characters).

  • Compound types: can group multiple values (e.g., tuples, arrays).

Rust is a statically typed language, which means data types are known at compile time. Here are some of the most common data types:

  1. Integer: i8, u8, i16, u16, i32, u32, i64, u64, isize, usize

  2. Float: f32, f64

  3. Boolean: true or false

  4. Character: char (supports Unicode)

  5. String: String, &str

Example:1

Example:2

Rust has a strong static type system. This means that the data type of every variable must be known at compile time. Here are the most common data types in Rust:

πŸ”’ Numeric Types

Rust provides several numeric types, including:

  • Integer Types: i32, u32, i64, etc.

  • Floating-Point Types: f32 and f64.

Example of using integer and floating-point types:

🌈 Integer Type

Integers are whole numbers, and Rust supports various integer types:

πŸ”’ Floating Point Type

Floating point numbers represent decimal values:

πŸ’‘ Boolean Type

The boolean type is represented as bool, which can hold either true or false.

πŸ”€ Character Type

The character type is represented as char, which holds a single character. It is defined with single quotes.

πŸ“ String Type

Rust has two types of strings:

  1. String: A mutable string type.

  2. &str: A string slice, which is immutable.

Example:

πŸ”— Compound Types

Compound types can group multiple values together.

πŸ“œ Tuples

Tuples are used to group a fixed number of values of different types:

πŸ“¦ Arrays

Arrays hold multiple values of the same type:

🎯 Hands-On Challenge

Create a Rust program that uses variables and demonstrates different data types. Your program should:

  1. Declare and print a variable holding your name.

  2. Create a mutable integer variable representing your current age and update it by adding one.

  3. Use a floating-point variable to store your favorite number and print it.

  4. Include a boolean variable that indicates whether you are learning Rust (set it to true).

  5. Use a character variable to store the first letter of your name.

Here's a basic template to get you started:

βœ… Share your solution on GitHub and tag #30DaysOfRust on social media! Let the world see your progress! πŸš€

πŸ’» Exercises - Day 2

βœ… Exercise: Level 1

  1. Declare a variable named my_age and set it to your age.

  2. Print the value of my_age to the console.

  3. Create a mutable variable named my_height and assign it your height in centimeters. Update it to a new height.

  4. Declare a variable my_name and assign it your name as a string. Print it to the console.

  5. Create a variable is_student and set it to true if you are a student, or false otherwise. Print the value.

  6. Create a variable birth_year and calculate your birth year by subtracting your age from the current year (you can use a hardcoded current year, e.g., 2024). Print the value.

βœ… Exercise: Level 2

  1. Create variables for each numeric type (integer and float) and print their values:

    • An integer variable my_integer set to any integer value.

    • A floating-point variable my_float set to any float value.

  2. Declare a boolean variable is_learning_rust and set it to true. Print the value.

  3. Create a character variable favorite_letter and assign it your favorite letter. Print it.

  4. Create an array of integers called my_scores that holds your last five test scores. Print the entire array.

  5. Create a string variable hobby and assign it one of your hobbies. Print it, and then concatenate it with another string to create a sentence (e.g., "I enjoy [hobby]!"). Print the complete sentence.

πŸŽ₯ Helpful Video References

πŸ“ Day 2 Summary

  • We explored variables in Rust, understanding the difference between immutable and mutable variables.

  • We covered the various data types available in Rust, including numeric types, boolean, character, and string.

  • We completed exercises to solidify our understanding of variables and data types.

🌟 Great job on completing Day 2! Keep practicing, and get ready for Day 3 where we will explore Control Flow in Rust!

Thank you for joining Day 2 of the 30 Days of Rust challenge! If you found this helpful, don’t forget to Star GIF star this repository, share it with your friends, and stay tuned for more exciting lessons ahead!

Stay Connected πŸ“§ Email: Hunterdii 🐦 Twitter: @HetPate94938685 🌐 Website: Working On It(Temporary)

<< Day 1 | Day 3 >>


Last updated