Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

:timing
:sccache 1

Tips

  1. Functions in a module is private by default. To make it public (so that it can called outside the module), you have to use the keyword pub.

fn add1(x: i32, y: i32) -> i32 {
    x + y
}
add1(1, 2)
3
let add2 = |x: i32, y: i32| x + y; 
add2(1, 2)
Sorry, the type [closure@src/lib.rs:112:12: 112:34] cannot currently be persisted

Function Overload

Rust does not support function overloading. There are multiple alternative ways to the OOP-style function overloading in Rust.

  1. Simply define different methods.

  2. Use trait to simulation function overloading.

Is there a simple way to overload functions?

Default/Optional Arguments

Rust does not support optinal arguments for Functions. For more discussions, please refer to Optional arguments in Rust .