0PricingLogin
Learn Rust Coding · Lesson

Functions and Control Flow

Learn to define functions, use `if`/`else`, `loop`, `while`, and `for` expressions to control program execution flow.

What are Functions?

Functions are named blocks of code that perform a specific task. They are fundamental for organizing your code and making it reusable.

  • Code Reusability: Write a piece of logic once and use it multiple times.
  • Modularity: Break down complex problems into smaller, manageable parts.
  • Readability: Give meaningful names to blocks of code, improving understanding.

Your First Rust Function

In Rust, you define a function using the fn keyword. The main function is the entry point of every Rust program.

Here's how to declare and call a simple function:

fn say_hello() {
    println!("Hello from a function!");
}

fn main() {
    say_hello(); // Call the function
    println!("Back in main.");
}

All lessons in this course

  1. Variables, Mutability, and Shadowing
  2. Primitive Data Types and Operators
  3. Functions and Control Flow
← Back to Learn Rust Coding