Rust 101 - Understanding the Basic Concepts of Rust

Rust 101 - Understanding the Basic Concepts of Rust

ยท

4 min read

As you may already know, Rust has a steep learning curve, which makes it difficult to learn. What if I told you there's a simpler way to learn Rust? The approach is to understand how data is stored and certain behaviors in Rust rather than learning everything at once.

In this article, we'll go over the fundamentals of Rust to get you started on your journey. The first thing to remember before you begin is that going fast is not always the ideal route.

Make use of Rust Playground or your preferred IDE to follow along.

Declaring a Variable in Rust

Rust relies heavily on variables. It is used to hold data. The let keyword is used to declare variables. If you don't add ; at the end of the line after declaring a variable, the compiler will complain.

fn main() {
    let a = true; 
    let b = "Rust";
    let c = 4;

    println!("{}", a);
    println!("{}", b);
    println!("{}", c);
}

We declared three variables and assigned values to them in the preceding code block. The data types for each variable were inferred automatically by the compiler. We logged the values in the console using the println! macro. The macro accepts two arguments: {} and the name of the variable.

Rust Data Types

There are a few data types available in Rust, and understanding them brings you one step closer to mastering the language. Because the language is statically typed, it is necessary to understand these types and their roles.

Scalar Types

In Rust, there are four main scalar types, which are used to represent single values.

  • Integer

  • Floating-Points

  • Characters

  • Booleans

Integer

In Rust, integers are whole numbers that can be signed or unsigned. Unsigned Integers can only store positive values, whereas Signed Integers can store both positive and negative values.

fn main() {
    // signed integers - i8, i16, i32, i64, i128, isize
    // unsigned integers - u8, u16, u32, u64, u128, usize

    let a = 1; // by default i32, this is known as an inferred type
    let b: i8 = -2; // you declare a by adding it to the variable'

    let c: u8 = 23; 

    println!("{}", a); // Printing to the console
    println!("{}", b);
    println!("{}", c);
}

The code block above highlights the types of signed and unsigned integers. If, after declaring a variable, you forget to include a type when defining it, the Rust compiler will do it on your behalf.

Printing integers to the console.

Floating-Points

Floats in Rust represent numbers with decimal points. There are mainly two types of floating points f32 and f64.



fn main() {
    let a = 11.11; // by default f64
    let b: f32 = 1.99; // you declare a type by adding it to the variable'

    println!("{}", a);
    println!("{}", b);
}

Using the let keyword, we declared two variables in the preceding code block. Also, if you forget to include a type when defining a variable, the Rust compiler will do it for you.

Characters

The char data type in Rust supports a range of values. It can accept numbers, special characters, and even emojis.


fn main() {
    let a: char = '&'; // you declare a type by adding it to the variable' 
    let b = '๐Ÿ˜€'; // char by default 
    let c: char = 'C';

    println!("{}", a);
    println!("{}", b);
    println!("{}", c);
}

Boolean

In general, Boolean data types are known to have only two values: true or false

fn main() {
    let a: bool = true; // you declare a type by adding it to the variable' 
    let b = false; // bool by default 

    println!("{}", a);
    println!("{}", b);
}

Conclusion

We covered how to declare variables and scalar data types in Rust in this article. We also talked about how to assign types to variables.

Meanwhile, if you want to learn more about Rust, I recommend the following resources:

ย