Rust Basics and Syntax
In this chapter, we will cover the fundamental concepts of Rust programming language. Rust is a systems programming language that prioritizes safety and performance.
Variables and Data Types
In Rust, you can declare variables using the let keyword. Rust is a statically typed language, which means it must know the data type of a variable at compile time. The basic data types in Rust are:
-
Integers:
i8,i16,i32,i64,i128,isize -
Floating point numbers:
f32,f64 -
Boolean:
bool -
Character:
char
Example:
let x: i32 = 10;
let y: f64 = 20.5;
Control Flow
Rust has the usual control flow statements such as if, else, loop, and while. The if statement is used to execute a block of code if a condition is true.
Example:
let x = 10;
if x > 5 {
println!("x is greater than 5");
}
Functions
In Rust, you can define functions using the fn keyword. Functions are used to organize code and reuse it.
Example:
fn greet(name: &str) {
println!("Hello, {}!", name);
}
fn main() {
greet("John");
}
By the end of this chapter, you will have a solid understanding of Rust basics and syntax, and be ready to move on to more advanced topics. Practice is key, so be sure to try out the examples and exercises to reinforce your learning.