Rust Functions

This chapter delves into rust-functions , which are fundamental building blocks for structuring code. Functions allow you to encapsulate logic, promote code reuse, and improve readability. Rust provides a robust framework for defining and working with functions, ensuring type safety and performance.

Chapter Goal

  • Understand the purpose and syntax of functions in Rust.
  • Learn how to define and call functions.
  • Explore the use of parameters, return values, and closures.

Basic Rules for Functions in Rust

  • Functions must be defined with the fn keyword followed by the function name.
  • All parameters must have explicit types; Rust does not infer parameter types.
  • Functions can return values, and the return type must be specified after the -> symbol.
  • The last expression in a function is automatically returned if no return statement is used.
  • Functions without a return value implicitly return () (the unit type).
  • Nested functions are not allowed; all functions must be defined at the module level.

Key Characteristics of Functions in Rust

  • Encapsulation: Functions group related logic into reusable units.
  • Type Safety: Parameters and return values have explicitly defined types.
  • Flexible Parameters: Support for mutable, immutable, and borrowed parameters.
  • Closures: Anonymous functions for concise logic.

Best Practices

  • Use descriptive names for functions and parameters.
  • Prefer small, single-responsibility functions for better maintainability.
  • Use closures for short-lived, inline logic.
  • Leverage Rust’s ownership model to manage function parameters effectively.

Syntax Table

Serial No Concept Syntax Example Description
1 Define a Function fn add(a: i32, b: i32) -> i32 { a + b } Defines a function with parameters and a return type.
2 Call a Function let sum = add(5, 10); Calls a function with arguments.
3 Return a Value fn square(n: i32) -> i32 { n * n } Returns a value using the -> syntax.
4 No Return Value fn greet() { println!(“Hello”); } Defines a function without a return value.
5 Closure `let add = a, b a + b;` Defines an anonymous function (closure).

Syntax Explanation

1. Define a Function

What is a Function?

A function is a block of code that performs a specific task. Functions can take input parameters, execute logic, and return a value.

Syntax

fn function_name(param1: Type1, param2: Type2) -> ReturnType {

    // Function body

}

Detailed Explanation

  • The fn keyword defines a function.
  • Parameters are listed in parentheses with their types.
  • The return type is specified after the -> symbol.
  • The function body contains the logic to execute.

Example

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

    a + b

}

 

fn main() {

    let result = add(5, 10);

    println!(“Sum: {}”, result);

}

Example Explanation

  • The program defines a function add that takes two i32 parameters and returns their sum.
  • The function is called in the main function with arguments 5 and 10.
  • The result is printed to the console.

2. Function Parameters and Return Values

What are Parameters and Return Values?

Parameters allow functions to accept input values, while return values provide the result of the function’s execution.

Syntax

fn multiply(a: i32, b: i32) -> i32 {

    a * b

}

Detailed Explanation

  • Parameters are defined with names and types.
  • The return keyword can be used, but the last expression in the function is returned by default.

Example

fn square(n: i32) -> i32 {

    n * n

}

 

fn main() {

    let result = square(4);

    println!(“Square: {}”, result);

}

Example Explanation

  • The program defines a function square that computes the square of an integer.
  • The function is called with the argument 4, and the result is printed.

3. No Return Value

What is a Function Without a Return Value?

Functions can execute logic without returning a value. These functions implicitly return () (the unit type).

Syntax

fn greet() {

    println!(“Hello!”);

}

Detailed Explanation

  • Functions without a return type do not use the -> syntax.
  • The () type indicates no value is returned.

Example

fn greet(name: &str) {

    println!(“Hello, {}!”, name);

}

 

fn main() {

    greet(“Alice”);

}

Example Explanation

  • The program defines a function greet that takes a string slice parameter.
  • It prints a greeting message without returning a value.

4. Closures

What are Closures?

Closures are anonymous functions that can capture variables from their environment. They are used for concise, inline logic.

Syntax

let closure = |param1, param2| param1 + param2;

Detailed Explanation

  • Closures are defined using the | syntax for parameters.
  • They can capture variables from the surrounding scope.

Example

fn main() {

    let add = |a, b| a + b;

    println!(“Sum: {}”, add(3, 4));

}

Example Explanation

  • The closure add takes two parameters and returns their sum.
  • It is called with arguments 3 and 4, and the result is printed.

Real-Life Project

Project Name: Temperature Converter

Project Goal: Use functions to convert temperatures between Celsius and Fahrenheit.

Code for This Project

fn celsius_to_fahrenheit(c: f64) -> f64 {

    (c * 9.0 / 5.0) + 32.0

}




fn fahrenheit_to_celsius(f: f64) -> f64 {

    (f - 32.0) * 5.0 / 9.0

}




fn main() {

    let temp_c = 25.0;

    let temp_f = 77.0;




    println!("{}°C is {:.2}°F", temp_c, celsius_to_fahrenheit(temp_c));

    println!("{}°F is {:.2}°C", temp_f, fahrenheit_to_celsius(temp_f));

}

Save and Run

  • Save the code in a file named main.rs.
  • Compile using rustc main.rs.
  • Run the executable: ./main.

Expected Output

25°C is 77.00°F

77°F is 25.00°C

Insights

  • Functions promote code reuse and modularity.
  • Closures provide a concise way to express logic in Rust.
  • Explicit type annotations improve clarity and prevent errors.

Key Takeaways

  • Use functions to encapsulate reusable logic.
  • Combine parameters and return values for flexible APIs.
  • Explore closures for short-lived, inline logic.