8.4 Default Parameter Values and Named Arguments

Rust does not support default parameter values or named arguments when calling functions.

  • Default Parameters: In some languages, you can specify default values for parameters so that callers can omit them. Rust does not support this feature.
  • Named Arguments: Some languages allow you to specify arguments by name when calling a function. Rust requires that arguments are provided in the order they are defined, without naming them.

Example of Non-Supported Syntax:

// This is not valid Rust
fn display(message: &str, repeat: u32 = 1) {
    for _ in 0..repeat {
        println!("{}", message);
    }
}

fn main() {
    display("Hello"); // Error: missing argument for `repeat`
    display("Hello", repeat: 3); // Error: named arguments not supported
}

Workaround Using Option Types or Builder Patterns

To achieve similar functionality, you can use Option<T> types for optional parameters or employ the builder pattern.

Using Option<T> for Optional Parameters

You can define parameters as Option<T>, allowing callers to pass None to use a default value.

fn display(message: &str, repeat: Option<u32>) {
    let times = repeat.unwrap_or(1);
    for _ in 0..times {
        println!("{}", message);
    }
}

fn main() {
    display("Hello", None);         // Uses default repeat of 1
    display("Hi", Some(3));         // Repeats 3 times
}
  • The unwrap_or method provides a default value if None is passed.
  • Callers must explicitly pass Some(value) or None.

Using the Builder Pattern

The builder pattern allows you to construct complex objects step by step. It's useful when you have many optional parameters.

struct DisplayConfig {
    message: String,
    repeat: u32,
}

impl DisplayConfig {
    fn new(message: &str) -> Self {
        DisplayConfig {
            message: message.to_string(),
            repeat: 1, // Default value
        }
    }

    fn repeat(mut self, times: u32) -> Self {
        self.repeat = times;
        self
    }

    fn show(&self) {
        for _ in 0..self.repeat {
            println!("{}", self.message);
        }
    }
}

fn main() {
    DisplayConfig::new("Hello").show();          // Uses default repeat of 1
    DisplayConfig::new("Hi").repeat(3).show();   // Repeats 3 times
}
  • The DisplayConfig struct acts as a builder.
  • Methods like repeat modify the configuration and return self, allowing method chaining.
  • This pattern provides flexibility similar to functions with default parameters and named arguments.