Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Module Functions

A module can contain functions that are accessible within the module. By declaring a function as public using the keyword pub, it becomes available for use outside the module.

test

// module math
mod math {
    // pow cannot be called from outside math
    fn pow(x: Scalar, n: Integer) -> Scalar {
        if n == 1 {
            x // return x
        } else {
            x * pow(x, n - 1) // return recursive product
        }
    }

    // square is callable from outside math
    pub fn square(x: Scalar) -> Scalar {
        // call internal pow
        pow(x, 2)
    }
}

// call square in math
math::square(2.0);
math::pow(2.0, 5); // error: pow is private