HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RustVariablesData TypesOperatorsStringsVectors (Vec)Conditions (if / match)Loops (for / while)FunctionsOwnership & Borrowing
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Vector & HashMap
🔁 Condition & Loop
🧩 Function
fn
🔑 Ownership & Borrowing
Rust›REFERENCE›Function
🧩

Function

Function definition with fn.

CommandDescription
fnDefines a function.

fn

Try it yourself →

Defines a function.

Syntax

fn name(p: type) -> returnType { ... }

Description

Parameter types are written with :, return types with ->. The last expression (without a semicolon) is returned automatically — no return needed.

Example

fn square(x: i32) -> i32 {
    x * x
}
fn main() {
    println!("{}", square(5));
}

Expected output

25

When to use?

Preventing code repetition.

Common mistakes

  • ⚠️ Do NOT put a ; at the end of the final returned expression: x * x (returns), x * x; (does not return).
HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code