Skip to content
Learn Netverks

Lesson

Step 9/36 25% through track

functions

Functions

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Functions: the syntax, APIs, and habits you need before advancing in Rust.

Functions keep scripts maintainable before you reach for classes and frameworks.

You will apply Functions in contexts like: Infrastructure CLIs, proxies, game engines, blockchain nodes, and latency-sensitive backends.

Write Rust with fn main(), click Run on server—the dev runner compiles main.rs with rustc and runs the binary; fix borrow errors from stderr (requires Rust toolchain; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Functions use fn, parameter types, and return types. The last expression without semicolon is the return value—use return for early exit.

Example

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

Important interview questions and answers

  1. Q: Expression vs statement?
    A: Expressions produce values; adding ; turns the last line into a statement returning ().

Self-check

  1. How do you return a value without return?

Ownership in parameters

Parameters can take ownership (String), borrow immutably (&T), or borrow mutably (&mut T). The signature documents the contract—callers know if they lose ownership.

Pitfall

Adding ; to the last expression returns unit () instead of the value you meant to return.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Return type required?
  • Expression body?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump