|
|
|
@ -259,13 +259,15 @@ types implement <code>Default</code>, the more useful it becomes.</p>
|
|
|
|
|
<code>default()</code> method does not. There can even be multiple constructors with
|
|
|
|
|
different names, but there can only be one <code>Default</code> implementation per type.</p>
|
|
|
|
|
<h2><a class="header" href="#example-2" id="example-2">Example</a></h2>
|
|
|
|
|
<pre><code class="language-rust ignore">// note that we can simply auto-derive Default here.
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
<pre><pre class="playground"><code class="language-rust edition2018">use std::{path::PathBuf, time::Duration};
|
|
|
|
|
|
|
|
|
|
// note that we can simply auto-derive Default here.
|
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
|
struct MyConfiguration {
|
|
|
|
|
// Option defaults to None
|
|
|
|
|
output: Option<Path>,
|
|
|
|
|
output: Option<PathBuf>,
|
|
|
|
|
// Vecs default to empty vector
|
|
|
|
|
search_path: Vec<Path>,
|
|
|
|
|
search_path: Vec<PathBuf>,
|
|
|
|
|
// Duration defaults to zero time
|
|
|
|
|
timeout: Duration,
|
|
|
|
|
// bool defaults to false
|
|
|
|
@ -279,9 +281,11 @@ impl MyConfiguration {
|
|
|
|
|
fn main() {
|
|
|
|
|
// construct a new instance with default values
|
|
|
|
|
let mut conf = MyConfiguration::default();
|
|
|
|
|
// do somthing with conf here
|
|
|
|
|
// do something with conf here
|
|
|
|
|
conf.check = true;
|
|
|
|
|
println!("conf = {:#?}", conf);
|
|
|
|
|
}
|
|
|
|
|
</code></pre>
|
|
|
|
|
</code></pre></pre>
|
|
|
|
|
<h2><a class="header" href="#see-also-1" id="see-also-1">See also</a></h2>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>The <a href="idioms/ctor.html">constructor</a> idiom is another way to generate instances that may or may
|
|
|
|
|