codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

4 / 58 topics
1Getting Started with Rust2Installing Rust3Hello World Program4Rust Syntax5Variables and Mutability6Data Types7Operators
Tutorials/Rust/Rust Syntax
🦀Rust

Rust Syntax

Updated 2026-04-20
3 min read

Rust Syntax

Rust is a systems programming language known for its performance, safety, and concurrency support. Understanding Rust's syntax is crucial for effectively writing safe and efficient code. This tutorial will cover the fundamental aspects of Rust syntax, including variables, data types, control flow, functions, and modules.

Variables

Variables in Rust are immutable by default. To make a variable mutable, you need to use the mut keyword.

let x = 5; // Immutable variable
let mut y = 10; // Mutable variable
y = 20;

Shadowing

Rust allows you to shadow variables, which means you can declare a new variable with the same name as an existing one. The new variable will shadow the old one.

let x = 5;
let x = x + 1; // Shadows the previous value of x

Data Types

Rust is a statically typed language, meaning that it must know the types of all variables at compile time. Rust has two main categories of data types: scalar and compound.

Scalar Types

Scalar types represent a single value. Rust has four primary scalar types:

  • Integers: i8, i16, i32, i64, i128 (signed) and u8, u16, u32, u64, u128 (unsigned).
  • Floating-point numbers: f32 and f64.
  • Booleans: bool (true or false).
  • Characters: char, representing a Unicode scalar value.
let integer: i32 = 42;
let float: f64 = 3.14;
let boolean: bool = true;
let character: char = 'A';

Compound Types

Compound types can group multiple values into one type. Rust has two primitive compound types:

  • Tuples: A general way of grouping a number of values with a variety of types into one compound type.
let tuple: (i32, f64, char) = (500, 6.4, 'z');
  • Arrays: A collection of elements of the same type stored in contiguous memory locations.
let array: [i32; 5] = [1, 2, 3, 4, 5];

Control Flow

Rust provides several control flow constructs similar to other programming languages.

Conditional Statements

The if statement is used for conditional execution. Rust does not have a ternary operator like some languages; instead, it uses if let for pattern matching.

let number = 3;
if number < 5 {
    println!("Condition was true");
} else {
    println!("Condition was false");
}

Loops

Rust has three types of loops: loop, while, and for.

  • Infinite loop:
loop {
    println!("This loop runs forever!");
}
  • While loop:
let mut number = 3;
while number != 0 {
    println!("{}!", number);
    number -= 1;
}
println!("LIFTOFF!!!");
  • For loop:
for element in array.iter() {
    println!("The value is: {}", element);
}

Functions

Functions are defined using the fn keyword. Rust functions have explicit return types, which can be inferred or specified.

fn add(a: i32, b: i32) -> i32 {
    a + b // The last expression is implicitly returned
}

let sum = add(5, 6);

Closures

Closures are anonymous functions that can capture their environment. They are defined using the | syntax.

let closure = |x: i32| x + 1;
println!("Closure result: {}", closure(5));

Modules and Packages

Rust uses a module system to organize code. Modules help in managing scope and encapsulation.

Defining Modules

Modules are defined using the mod keyword. You can nest modules within other modules.

// main.rs
mod my_module {
    pub fn public_function() {
        println!("This is a public function");
    }

    fn private_function() {
        println!("This is a private function");
    }
}

fn main() {
    my_module::public_function();
}

Using Modules

You can use modules in other files by declaring them with mod and using the use keyword.

// lib.rs
pub mod my_module;

// main.rs
use crate::my_module::public_function;

fn main() {
    public_function();
}

Best Practices

  • Use immutable variables by default to avoid accidental changes.
  • Shadowing can be useful, but overusing it can lead to confusion. Use it judiciously.
  • Explicit return types improve code readability and help catch errors early.
  • Avoid deep nesting of modules to maintain a clean project structure.

Conclusion

Rust's syntax is designed to provide safety and performance while maintaining flexibility. By understanding the basics covered in this tutorial, you can start writing robust and efficient Rust programs. As you progress, explore more advanced features like lifetimes, traits, and generics to fully leverage Rust's capabilities.


PreviousHello World ProgramNext Variables and Mutability

Recommended Gear

Hello World ProgramVariables and Mutability