#default-value #default #try #options #present #version #wrapping

nightly try_default

A trait to optionally get the Default if present, or None if there is no Default

3 stable releases

2.0.0 Aug 19, 2024
1.0.1 Oct 14, 2023
1.0.0 Mar 8, 2021

#5 in #present

Download history 130/week @ 2024-08-14 22/week @ 2024-08-21 8/week @ 2024-09-11 14/week @ 2024-09-18 25/week @ 2024-09-25 8/week @ 2024-10-02

360 downloads per month
Used in 2 crates

MIT license

5KB
68 lines

TryDefault

Current Crates.io Version

A 'try' version of Default, where it returns an Option::Some wrapping the default value when present. It returns Option::None when there is no Default available.

You can in theory call this on any type to retrive it's Default, if there is one!

use ::try_default::TryDefault;

// Set to `Some(0)`.
let default_num = <u32>::try_default();

// Set to `None`, as `::std::fs::File` has no `Default`.
let default_file = <::std::fs::File>::try_default();

Notes

  • Requires nightly for specialization feature.
  • Is possibly the world's most pointless crate. But it's here if you need it!

No runtime deps