Near Learn

If, loops & match

if/else and loops carry over directly. Rust adds match (a superpowered switch) and makes if an expression that returns a value.

Beginner4 min read3-question check

Control flow is familiar territory. if / else and loops read almost the same; the new tools are match (pattern-matching switch) and the fact that if and match are expressions — they return a value.

Classify a number
Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract Flow {
    function classify(uint32 n) external pure returns (string memory) {
        if (n == 0) {
            return "zero";
        }

        uint32 sum = 0;
        for (uint32 i = 1; i <= n; i++) {
            sum += i;
        }

        if (sum % 2 == 0) {
            return "even";
        } else {
            return "odd";
        }
    }
}
Rust
use near_sdk::near;

#[near(contract_state)]
#[derive(Default)]
pub struct Flow {}

#[near]
impl Flow {
    pub fn classify(&self, n: u32) -> String {
        if n == 0 {
            return "zero".to_string();
        }

        let mut sum = 0u32;
        for i in 1..=n {
            sum += i;
        }

        // match is a switch that returns a value
        match sum % 2 {
            0 => "even".to_string(),
            _ => "odd".to_string(),
        }
    }
}
Coming from EVM

for (uint i; i < n; i++) ↔ for i in 0..n (or 1..=n for inclusive). while, break and continue work as you expect; loop is an explicit infinite loop. What Rust lacks is the C-style for (init; cond; step) header — for always iterates over a range or a collection.

match is switch with exhaustiveness: the compiler makes you handle every case (_ is the default). Because it is an expression, the whole match evaluates to the value you return.

A function’s last expression (no semicolon) is its return value, so many methods end with a match or a value instead of an explicit return.

Check yourself

3 questions · progress saved in this browser

  1. 1.What does Rust's match add over a Solidity if / else chain?
  2. 2.What is the Rust equivalent of for (uint32 i = 1; i <= n; i++)?
  3. 3.A method ends with match sum % 2 { 0 => "even".to_string(), _ => "odd".to_string() } with no return and no trailing semicolon. What does it return?