pub trait ConstDefault: Sized {
    const DEFAULT: Self;
}
Available on crate feature const_default only.
Expand description

A const equivalent of the Default trait.

This trait can be derived with the ConstDefault derive macro (requires the “derive” feature).

Features

Enabling the “rust_1_51” feature allows arrays of all lengths to implement this trait, otherwise it’s only implemented for arrays up to 32 elements long.

Example

Manually implementing ConstDefault for a struct

use core_extensions::{ConstDefault, const_default};
 
#[derive(Debug,PartialEq)]
struct Point<T>{
    x: T,
    y: T,
}

impl<T> ConstDefault for Point<T>
where
    T: ConstDefault
{
    const DEFAULT: Self = Point {
        // `const_default!()` is equivalent to `ConstDefault::DEFAULT`
        x: const_default!(),
        y: const_default!(),
    };
}

assert_eq!(const_default!(Point<u8>), Point{x: 0, y: 0});
assert_eq!(const_default!(Point<f32>), Point{x: 0.0, y: 0.0});
assert_eq!(const_default!(Point<Option<()>>), Point{x: None, y: None});

Required Associated Constants

The default value for Self.

Implementations on Foreign Types

When the “const_params” feature is disabled, the ConstDefault trait is implemented for arrays up to 32 elements long.

Implementors