RRust By Example
beginnerFunctions

Functions Basic

Define and call functions with parameters and return values.

Functions Basic

Define and call functions with parameters and return values.

Difficulty

Beginner

Code

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

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

fn main() {
    let result = add(3, 5);
    println!("3 + 5 = {}", result);
    greet("Rust");
}

Explanation

This example demonstrates how to use functions basic in Rust. Read the code carefully to understand the flow. Pay attention to where values are created, borrowed, moved, or consumed.

Key Concepts

  • Rust's strong type system catches errors at compile time
  • Ownership and borrowing rules ensure memory safety
  • Pattern matching makes code expressive and exhaustive

Related Topics

Browse more examples in the functions category to build a complete understanding of this topic.

More Functions Examples