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

Scoped functions

  • We can defined a function inside a function and then it will be only available in that scope.
  • Surprisingly (or not) we can call the function anywhere inside the scope, even before its declaration.
fn main() {
    say_hi("before");

    fn say_hi(text: &str) {
        println!("hi {text}");
    }

    say_hi("after");

    other();
}

fn other() {
    println!("in other");

    // cannot find function `say_hi` in this scope
    //say_hi("other");
}
hi before
hi after
in other