“Rust Basics: Syntax and Data Structures”

META

Activist
SUPREME
MEMBER
Joined
Mar 1, 2026
Messages
118
Reaction score
378
Deposit
0$
Rust is a language that has been becoming more or less popular in recent years due to its high performance and safety. It was developed by Mozilla and a community of developers with the goal of providing a tool for systems programming that helps avoid many common memory management errors.

In this article, we will look at the basics of Rust.


---

Environment Setup

On the Rust website, there are instructions for installing Rust on any OS. The installation process is simple and intuitive and can be completed even by a school student.

After installing Rust and Cargo, let’s make sure they work correctly. Open a terminal and run:

rustc --version

This command should return the current version of the Rust compiler installed on your PC.

Check the Cargo version:

cargo --version

If both commands return versions successfully, everything is set up correctly.


---

Creating Your First Rust Program

To create a new Rust project, you can use:

cargo new hello_rust

Cargo is a universal dependency management tool in Rust. This command creates a directory named hello_rust containing the project structure. Inside it, there is a main.rs file where we write our code.

Let’s start with the classic “Hello, World!”:

fn main() {
println!("Hello, World!");
}

Then go to the project directory in the terminal and run:

cargo run


---

Rust Syntax

Variables and Mutability

One of Rust’s features is its strict type system, which helps avoid many errors at compile time:

let x = 42; // Immutable variable
let mut y = 24; // Mutable variable

let is used to declare variables. The first variable x cannot be changed after assignment, while y can.


---

Operators and Control Flow

Rust supports basic operators: arithmetic, logical, and comparison:

fn ariph() {
let a = 10;
let b = 3;

let sum = a + b;
let difference = a - b;
let product = a * b;
let quotient = a / b;
let remainder = a % b;
}

fn logic() {
let is_sunny = true;
let is_warm = false;

let is_good_weather = is_sunny && is_warm;
let is_raining = !is_sunny;
}

fn sravn() {
let x = 5;
let y = 10;

let is_equal = x == y;
let is_not_equal = x != y;
let is_greater = x > y;
let is_less = x < y;
let is_greater_or_equal = x >= y;
let is_less_or_equal = x <= y;
}


---

Conditional Statements

fn main() {
let age = 25;

if age < 18 {
println!("You are a minor");
} else if age < 65 {
println!("You are an adult");
} else {
println!("You are a senior");
}
}


---

Loops

fn main() {
for i in 1..=5 {
println!("Iteration {}", i);
}

let mut counter = 0;
while counter < 3 {
println!("Repeat {}", counter);
counter += 1;
}
}


---

Match Expressions

fn main() {
let language = "Rust";

match language {
"Rust" => println!("You chose Rust — great choice!"),
"Python" | "JavaScript" => println!("Also good languages!"),
_ => println!("Unknown programming language"),
}
}


---

Functions

fn main() {
greet("Alice");

let result = add(3, 5);
println!("Sum: {}", result);
}

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

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


---

Data Types

Integers

let integer: i32 = 42;
let unsigned_integer: u64 = 100;

Floating-point numbers

let float: f32 = 3.14;
let double: f64 = 2.71;

Booleans

let is_rust_cool: bool = true;
let is_open_source: bool = false;

Characters and strings

let char_type: char = 'A';
let string_type: &str = "Hello, Rust!";


---

Vectors

fn main() {
let mut numbers: Vec<i32> = Vec::new();

numbers.push(42);
numbers.push(24);

for num in &numbers {
println!("Number: {}", num);
}
}


---

Arrays

fn main() {
let colors: [&str; 3] = ["Red", "Green", "Blue"];

for color in &colors {
println!("Color: {}", color);
}
}

Arrays have a fixed size known at compile time.


---

Structs

struct Point {
x: f64,
y: f64,
}

fn main() {
let origin = Point { x: 0.0, y: 0.0 };
println!("({}, {})", origin.x, origin.y);
}


---

Enums

enum Animal {
Dog,
Cat,
Bird,
}

fn main() {
let pet = Animal::Dog;

match pet {
Animal::Dog => println!("This is a dog!"),
Animal::Cat => println!("This is a cat!"),
Animal::Bird => println!("This is a bird!"),
}
}


---

If you need to group data with different fields, use a struct. If you need to represent states or variants, enums are a great choice.

Rust also provides safe memory management without a garbage collector.


---

Conclusion

Overall, Rust is a very solid programming language and continues to grow in popularity.


---
 
Top Bottom