Function Default value for argument in Rust 1.55

Find this useful? Support us: Star on GitHub 6
Category: Error handling | Language: Rust 1.55

In Rust, default values for function arguments are not supported directly. However, it is possible to achieve the same behavior using some workarounds.

One way to accomplish this is to use the Option enum type. You can define the argument as an Option and set a default value of None, then within the function, you can check whether the argument has a value or if it is None, in which case you can use the default value.

Here's an example:

fn greet(name: Option<&str>) {
    let username = name.unwrap_or("Anonymous");
    println!("Hello, {}!", username);
}

fn main() {
greet(Some("Bob")); // Output: Hello, Bob!
greet(None); // Output: Hello, Anonymous!
}

In this example, the greet function takes an Option<&str> argument name and sets the default value to None. Within the function, we use the unwrap_or method to get the value of name if it is Some, or use the default value of "Anonymous" if it is None.

Another approach is to use function overloading. In Rust, you can define multiple functions with the same name but with different argument signatures. By defining a version of the function that takes fewer arguments and sets default values, you can achieve the same effect as default arguments.

Here's an example:

fn greet() {
    greet_with_name("Anonymous");
}

fn greet_with_name(name: &str) {
println!("Hello, {}!", name);
}

fn main() {
greet(); // Output: Hello, Anonymous!
greet_with_name("Bob"); // Output: Hello, Bob!
}

In this example, we define two versions of the greet function. The first function, with no arguments, calls the greet_with_name function with the default value of "Anonymous". The greet_with_name function takes a &str argument and prints a greeting message using the provided name.

By calling the greet function with no arguments, we effectively use the default value of "Anonymous".

These are two ways to achieve the behavior of default arguments in Rust.