try_default 1.0.0

A trait to optionally get the Default if present, or None if there is no Default.
  • Coverage
  • 66.67%
    2 out of 3 items documented1 out of 3 items with examples
  • Links
  • Dependencies
  • Versions
  • Owners
  • JosephLenton


Current 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();


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