Chapter 17: Rust Functions

Rust Functions.

This is a big milestone! Functions are where code starts to become reusable, organized, and real-world useful. In Rust, functions feel very clean and strict — types everywhere, explicit returns, no hidden magic. Rust forces you to think about ownership & borrowing even in simple functions, which is why it feels “different” at first but becomes super powerful later.

Let me explain like your patient teacher sitting next to you with VS Code open: slowly, from basic to advanced, with lots of copy-paste examples you can run right now, Hyderabad-flavored comments, and comparisons so you see why Rust does things this way.

1. Basic Function Syntax – Defining & Calling

Every function starts with fn keyword.

Rust
  • fn = keyword to define function
  • say_namaste = name (snake_case convention — all lowercase + underscores)
  • () = parentheses — always required, even with no parameters
  • { … } = body
  • Call it by name + ()

Rust doesn’t care where you define functions — above or below main is fine.

2. Functions with Parameters (Arguments)

Parameters = special variables inside the function. You must write their types (no inference here — safety!).

Rust
  • name: &str — type after colon
  • Multiple params separated by comma
  • &str = string slice (cheap borrow — very common for text)

3. Returning Values – Two Styles

Rust functions can return values. Return type after ->.

Style 1: Explicit return (like most languages)

Rust

Style 2: Implicit return (Rust favorite — no return, no 😉

Rust
  • Last line without ; = return value
  • You can mix — return early, implicit at end

Full example:

Rust

4. Functions with Ownership & Borrowing (Rust Magic Starts Here)

Remember ownership? Functions take ownership or borrow.

Rust
  • String (no &) → move ownership
  • &String → immutable borrow
  • &mut String → mutable borrow (only one at a time!)

5. Returning Multiple Values (Tuples!)

Rust

Destructuring makes it clean.

6. Function Signatures & Documentation (Good Practice)

Rust
  • /// = doc comment (shows in cargo doc)
  • Parameters & return type mandatory in signature

7. Associated Functions (Like static methods)

Rust

Teacher Summary Table

Feature Syntax Example Key Notes
No params, no return fn hello() { … } Always ()
Params fn greet(name: &str) Types required
Return value -> i32 Implicit last expr or return
Ownership transfer fn take(s: String) Moves — caller loses it
Borrow immutable fn len(s: &String) -> usize Safe read-only
Borrow mutable fn increment(n: &mut i32) One mutable borrow only
Multiple returns -> (i32, i32) Use tuple + destructuring
Method fn area(&self) in impl self, &self, &mut self

Practice tip: Create cargo new rust_functions Write 5–6 small functions:

  • greet with name & city
  • calculate circle area
  • is_even checker
  • swap two numbers (return tuple)
  • temperature converter (°C → °F)

Want to go deeper next?

  • Methods vs functions in detail?
  • Closures (anonymous functions)?
  • Ownership & borrowing inside functions (the heart of Rust)?
  • Or generics in functions?

Just tell me — your Rust class from Hyderabad is going strong! 🦀🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *