Implementing Default
🎯 In Python, you set default values directly in __init__:
class AppSettings:
def __init__(self, name="myapp", debug=False, timeout=30):
self.name = name
self.debug = debug
self.timeout = timeout
settings = AppSettings() # all defaults
custom = AppSettings(debug=True) # override one, keep rest
Simple and flexible. Override what you want, keep defaults for the rest.
Rust's equivalent is the Default trait. It provides a default() method that creates an instance with default values:
let settings = AppSettings::default();
Deriving Default
When zero/empty values are fine, derive it:
#[derive(Default)]
struct Coordinate {
x: f64, // defaults to 0.0
y: f64, // defaults to 0.0
}
let origin = Coordinate::default();
assert_eq!(origin.x, 0.0);
Derived Default uses each field type's default: 0 for numbers, false for bool, "" for String, None for Option<T>. This is like Python's @dataclass with field(default=0), except the defaults come from the type system rather than explicit values.
Manual Default for meaningful values
Zero isn't always the right default. An app should probably default to a meaningful name, not an empty string:
impl Default for AppSettings {
fn default() -> Self {
AppSettings {
name: String::from("myapp"),
debug: false,
timeout_secs: 30,
}
}
}
The default() method returns Self — a fully constructed instance. This is like Python's __init__ with keyword defaults, but expressed as a separate trait rather than constructor parameters.
Struct update syntax: Rust's version of keyword defaults
Python lets you override individual keyword arguments. Rust has something similar — the struct update syntax with ..:
let custom = AppSettings {
debug: true,
..Default::default() // fill the rest with defaults
};
// name = "myapp", debug = true, timeout_secs = 30
The ..Default::default() fills in any fields you didn't specify. It reads like "start with the default, but change debug to true." This is the closest Rust gets to Python's keyword-argument overrides.
You can also use it with any existing instance:
let base = AppSettings::default();
let custom = AppSettings {
debug: true,
..base // fill rest from `base` instead of default
};
Note: ..base moves base's fields, so base can't be used afterward (unless all moved fields are Copy).
Where Default shows up
Default integrates with the standard library in useful ways:
Option::unwrap_or_default()— returns the default ifNoneHashMap::entry().or_default()— inserts default if key missing- Generic code can require
T: Defaultto create instances without knowing the concrete type
Login to see the full task and start coding.
Topics
This is a premium exercise
Log in to unlock the full exercise and start coding.
Login to access this exercise