Values and types · 10 min · free lesson

JavaScript functions vs fn

A Rust function declares every parameter type and returns its final expression; there are no default parameters and no implicit coercions.

Core idea

Every parameter has a type, arguments must match it exactly, and a block's final expression without a semicolon is its value.

JavaScript (Node.js) and Rust, side by side

JavaScript (Node.js) An implicit return in the arrow function, a default parameter, and a string silently coerced to a number.
const double = (x) => x * 2;      // expression body: implicit return

function shippingCost(weightKg, express = false) {
  const base = weightKg <= 1 ? 5 : 5 + (weightKg - 1) * 2;
  return express ? base * 2 : base;
}

console.log(double(21));
console.log(shippingCost(3), shippingCost(3, true));
console.log(shippingCost("3"));   // "3" - 1 is coerced: prints 9
Rust No return keywords: each body ends in an expression, and if replaces the ternary.
fn double(x: i32) -> i32 {
    x * 2
}

fn shipping_cost(weight_kg: u32, express: bool) -> u32 {
    let base = if weight_kg <= 1 { 5 } else { 5 + (weight_kg - 1) * 2 };
    if express { base * 2 } else { base }
}

fn main() {
    println!("{}", double(21));
    println!("{} {}", shipping_cost(3, false), shipping_cost(3, true));
}
What changes
JavaScript (Node.js)Rust
(x) => x * 2 returns its expression; { x * 2; } returns undefined.{ x * 2 } returns x * 2; { x * 2; } evaluates to () and fails a -> i32 signature.
function f(express = false) has a default.No default parameters: pass false, write a second function, or take an Option<bool>.
shippingCost("3") coerces the string and returns 9.A &str argument for a u32 parameter is E0308 at the call.
cond ? a : bif cond { a } else { b }, an expression with a single type.

Understand it

Arrow functions taught you expression bodies: (x) => x * 2 returns without return. Rust applies that idea to every block. The last expression of a function body, without a trailing semicolon, is the return value. A semicolon turns it into a statement whose value is (), the unit type, so fn double(x: i32) -> i32 { x * 2; } is a type error. return still exists for early exits.

Because if and match are expressions, there is no need for the ternary operator: let base = if weight_kg <= 1 { 5 } else { 5 + (weight_kg - 1) * 2 };. A block { ... } is an expression too, which is how a function body produces its value.

Parameters always have types, and the compiler never coerces arguments. JavaScript happily computed shippingCost("3") as 9 through "3" - 1; Rust rejects a &str where a u32 is expected, at the call site. There are no default or optional parameters and no overloading. Pass an explicit value, add a second function such as discounted_default, take an Option<T>, or group options in a struct that implements Default.

The return type is part of the signature and is never inferred from the body. A function's contract stays readable without its body, and rustc can point at the exact semicolon that turned an i32 into ().

The JavaScript (Node.js) habit, written in Rust

This is how the JavaScript (Node.js) version usually looks on a first attempt in Rust. Predict what rustc says before you read the explanation.

fn double(x: i32) -> i32 {
    x * 2;
}

fn main() {
    println!("{}", double(21));
}

Practise this code in Playground ↗

Try it

Port the JavaScript discounted shown at the top of solution.rs. Rust has no default parameters, so write discounted(price_cents, percent) and a separate discounted_default(price_cents) that applies 10 percent. A percent above 100 returns 0; otherwise subtract the discount, rounded down. In the Crabrix app, open the compiler lab and complete the supplied functions in solution.rs. The reading question is a separate exercise.

What success looks like: The local compiler accepts both functions and the separate verification harness passes every case, including a percent above 100 and the default. Printing a success message alone does not complete this lab.

Check your understanding

Why is fn double(x: i32) -> i32 { x * 2; } rejected?

Reveal the answer
  1. Rust needs return in every function
  2. x * 2 must be wrapped in parentheses
  3. The semicolon makes the body evaluate to (), which is not i32

With the semicolon, x * 2 is a statement and the block's value is (). rustc reports E0308 and suggests removing the semicolon.

Go deeper

Common misconception: “Without return, a Rust function returns nothing, like a JavaScript function body without return.”

A Rust block returns its final expression when it has no trailing semicolon. Only a semicolon, or an empty body, makes the value ().

  1. Read the signature: shipping_cost(weight_kg: u32, express: bool) -> u32 names every input type and the output. Both arguments are required.
  2. Find the tail values: let base = if ... { 5 } else { ... }; ends with a semicolon because it is a statement. The final if express { base * 2 } else { base } has none, so it is the return value.
  3. Compare with evidence: Run it: 42, then 9 18. Add ; after the final if and rustc reports E0308, expected u32, found ().

Transfer challenge: Call shipping_cost("3", false) and read where rustc places the error. Then rewrite shipping_cost with an early return 5; for light parcels and decide which version reads better.

Connections

  • Number vs Rust integer types: Explicit numeric types and overflow
  • Truthiness vs bool, if and match: No truthiness: conditions are bool
Side-by-side comparison: the JavaScript (Node.js) version on the left, the Rust version on the right, then 4 rows naming what changes.
JavaScript functions vs fn: the same task in JavaScript (Node.js) and in Rust, with the differences that matter.
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: JavaScript functions vs fn (courseId "rust-for-nodejs-developers", lessonId "node-functions-to-fn")
Link: https://crabrix.com/learn/rust-for-nodejs-developers/node-functions-to-fn/
Goal: Every parameter has a type, arguments must match it exactly, and a block's final expression without a semicolon is its value.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=rust-for-nodejs-developers/node-functions-to-fn) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.