1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-30 01:16:17 +02:00
comprehensive-rust/src/basic-syntax/functions-interlude.md
2022-12-21 16:38:28 +01:00

619 B

Function Overloading

Overloading is not supported:

  • Each function has a single implementation:
    • Always takes a fixed number of parameters.
    • Always takes a single set of parameter types.
  • Default values are not supported:
    • All call sites have the same number of arguments.
    • Macros are sometimes used as an alternative.

However, function parameters can be generic:

fn pick_one<T>(a: T, b: T) -> T {
    if std::process::id() % 2 == 0 { a } else { b }
}

fn main() {
    println!("coin toss: {}", pick_one("heads", "tails"));
    println!("cash prize: {}", pick_one(500, 1000));
}