gh-pages
simonsan 4 years ago
parent a33a8858ff
commit e9e0bedf3a

@ -182,13 +182,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" id="example">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&lt;Path&gt;,
output: Option&lt;PathBuf&gt;,
// Vecs default to empty vector
search_path: Vec&lt;Path&gt;,
search_path: Vec&lt;PathBuf&gt;,
// Duration defaults to zero time
timeout: Duration,
// bool defaults to false
@ -202,9 +204,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!(&quot;conf = {:#?}&quot;, conf);
}
</code></pre>
</code></pre></pre>
<h2><a class="header" href="#see-also" id="see-also">See also</a></h2>
<ul>
<li>The <a href="ctor.html">constructor</a> idiom is another way to generate instances that may or may

@ -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&lt;Path&gt;,
output: Option&lt;PathBuf&gt;,
// Vecs default to empty vector
search_path: Vec&lt;Path&gt;,
search_path: Vec&lt;PathBuf&gt;,
// 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!(&quot;conf = {:#?}&quot;, 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

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save