I found an unusual behavior of std::default
. If you create a nested structure with default setters and try to create the highest level struct with default parameters, it causes a stack overflow.
This code compiles, but when you try to run it throws thread '<main>' has overflowed its stack
:
use std::default;
pub struct A {
x: i32
}
impl Default for A {
fn default() -> A {
A { ..Default::default() }
}
}
fn main() {
let test = A { ..Default::default() };
}
But if you set the defaults of inherited by props, it works:
use std::default;
pub struct A {
x: i32
}
impl Default for A {
fn default() -> A {
A { x: 0 }
}
}
fn main() {
let test = A { ..Default::default() };
}
Is this a known issue and should I post it to the Rust issue tracker? My compiler version is rustc 1.2.0-nightly (0250ff9a5 2015-06-17)
.
Of course it causes a stack overflow.
To make this clearer, let's change the example a little:
When you say
A { ..Default::default() }
, you are not saying "create me anA
and executeDefault::default()
for each field". This means that the implementation ofdefault
is not equivalent to:It is, in fact, this:
So yes, it causes a stack overflow: you've defined the default value of
A
in terms of itself.