Radar · Rust · Archive · Week 13 · Mar 23 – 29, 2026
Option::map_or_default and Result::map_or_default RFC
Worth knowinglanguage
- What changed
- The RFC adds
Option::map_or_defaultandResult::map_or_default, which act like.map_or_else(Default::default, _). - Production impact
- The source does not say.
- Try it
- Look at the RFC to understand the new methods.
- Source
- github.com/rust-lang/rfcs/pull/3148
Understand it, then run it
Option::map_or_default and Result::map_or_default were added to the standard library. They let you write opt.map_or_default(f) instead of the longer opt.map_or_else(Default::default, f). The idea is the same as unwrap_or_default: if the value is present, apply a function; otherwise use the type’s default. This keeps code a bit shorter and clearer when you want the default value.
Run it now
// This program demonstrates the new Option::map_or_default and Result::map_or_default.
// It is written for Rust 1.98.1 (edition 2024) and compiles as is.
fn main() {
// Example with Option
let opt_some: Option<i32> = Some(10);
let opt_none: Option<i32> = None;
// Using map_or_default: if Some, double the value; if None, use i32::default() which is 0.
let doubled = opt_some.map_or_default(|v| v * 2);
let defaulted = opt_none.map_or_default(|v| v * 2);
println!("doubled: {}", doubled); // prints 20
println!("defaulted: {}", defaulted); // prints 0
// Example with Result
let res_ok: Result<&str, &str> = Ok("hello");
let res_err: Result<&str, &str> = Err("error");
// map_or_default on Result: if Ok, convert to uppercase; if Err, use default &str which is "".
let uppercased = res_ok.map_or_default(|s| s.to_uppercase());
let defaulted_err = res_err.map_or_default(|s| s.to_uppercase());
println!("uppercased: {}", uppercased); // prints HELLO
println!("defaulted_err: {}", defaulted_err); // prints empty string
}
What it printed when we ran it on Rust 1.98.1 (edition 2024)
doubled: 20 defaulted: 0 uppercased: HELLO defaulted_err:
After the change ships
rust · the proposal's code; it does not compile until the change ships
// This does not compile until the change ships.
// Option::map_or_default and Result::map_or_default are now part of the standard library.
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed