Rust

Understanding Rust basics: a friendly beginner's guide

Understanding Rust basics: a friendly beginner's guide

Rust is an efficient and reliable systems programming language that has gained popularity in recent years. If you're new to Rust, this article will help you get started by introducing its syntax, data types, variables, constants, functions, and control flow structures. We'll provide examples and personal insights along the way to make your learning experience enjoyable and engaging.

Syntax and data types

Rust has a clean and readable syntax, which makes it easy to learn for programmers of all skill levels. Let's start by looking at its fundamental data types:

  1. Integers: Rust has two types of integers: signed (i8, i16, i32, i64, i128) and unsigned (u8, u16, u32, u64, u128). The numbers represent the number of bits in the integer.
let x: i32 = 42; // signed 32-bit integer
let y: u64 = 128; // unsigned 64-bit integer
  1. Floating-Point Numbers: Rust has two floating-point types: f32 and f64, which are 32-bit and 64-bit, respectively.
  let x: f32 = 3.14; // 32-bit floating-point number
  let y: f64 = 2.718; // 64-bit floating-point number
  1. Booleans: Rust has a built-in boolean data type with two possible values: true and false.
let is_rainy: bool = false;
  1. Characters: Rust has a char data type for representing Unicode scalar values.
let c: char = 'A';
  1. Tuples: Rust tuples are collections of values with different data types. Tuples have a fixed length and are created using parentheses.
let point: (i32, i32) = (0, 0);
  1. Arrays: Arrays in Rust are fixed-length collections of elements of the same data type.
let arr: [i32; 5] = [1, 2, 3, 4, 5];

Variables and constants

Rust variables are immutable by default, which means you cannot change their values after they are initialized. If you need a mutable variable, you can use the mut keyword.

let x = 5; // immutable variable
let mut y = 6; // mutable variable
y = 7; // changing the value of a mutable variable

Constants in Rust are always immutable and can be declared using the const keyword. They require a type annotation, and their value must be a constant expression.

const MAXIMUM: u32 = 100;

Functions and control flow structures

Rust functions are defined using the fn keyword, followed by the function name, a parenthesized list of input parameters, a return type (if applicable), and a code block.

fn greet(name: &str) {
  println!("Hello, {}!", name);
}

fn add(a: i32, b: i32) -> i32 {
  a + b
}

Rust has a variety of control flow structures, including if statements, match expressions, and loops (loop, while, and for). Hereare some examples of control flow structures in Rust:

  1. If statements: Rust's if statements are used to execute code based on a condition.
let x = 7;

if x < 10 {
  println!("x is less than 10");
} else {
  println!("x is greater than or equal to 10");
}
  1. Match expressions: Rust's match expressions are similar to a switch statement in other languages, allowing you to compare a value against several patterns and execute the associated code block.
let x = 3;

match x {
  1 => println!("One"),
  2 => println!("Two"),
  3 => println!("Three"),
  _ => println!("Other"),
}
  1. Loops: Rust has three types of loops: loop, while, and for.
  • Loop: The loop keyword creates an infinite loop. You can use the break keyword to exit the loop.
let mut counter = 0;

loop {
  println!("Hello, world!");
  counter += 1;

  if counter >= 5 {
    break;
  }
}
  • While: The while loop executes a block of code as long as a condition is true.
let mut counter = 0;

while counter < 5 {
  println!("Hello, world!");
  counter += 1;
}
  • For: The for loop is used to iterate over a collection, such as an array or range.
let arr = [1, 2, 3, 4, 5];

for element in arr.iter() {
  println!("The value is: {}", element);
}

for number in 1..6 {
  println!("The number is: {}", number);
}

Conclusion

In conclusion, Rust is an increasingly popular systems programming language with a strong focus on safety, speed, and concurrency. By understanding its syntax, data types, variables, constants, functions, and control flow structures, you can start writing Rust programs with confidence. Remember that practice makes perfect, so keep experimenting and learning as you explore the world of Rust programming.

A blog for self-taught engineers

Сommunity is filled with like-minded individuals who are passionate about learning and growing as engineers.