You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-12-21 22:38:40 +02:00
Default trait slides cleanup (#1038)
* Default trait slides cleanup * Update src/traits/default.md Co-authored-by: Martin Geisler <martin@geisler.net> --------- Co-authored-by: Martin Geisler <martin@geisler.net>
This commit is contained in:
@@ -20,12 +20,12 @@ impl Default for Implemented {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let default_struct: Derived = Default::default();
|
let default_struct = Derived::default();
|
||||||
println!("{default_struct:#?}");
|
println!("{default_struct:#?}");
|
||||||
|
|
||||||
let almost_default_struct = Derived {
|
let almost_default_struct = Derived {
|
||||||
y: "Y is set!".into(),
|
y: "Y is set!".into(),
|
||||||
..Default::default()
|
..Derived::default()
|
||||||
};
|
};
|
||||||
println!("{almost_default_struct:#?}");
|
println!("{almost_default_struct:#?}");
|
||||||
|
|
||||||
@@ -43,7 +43,9 @@ fn main() {
|
|||||||
* Standard Rust types often implement `Default` with reasonable values (e.g. `0`, `""`, etc).
|
* Standard Rust types often implement `Default` with reasonable values (e.g. `0`, `""`, etc).
|
||||||
* The partial struct copy works nicely with default.
|
* The partial struct copy works nicely with default.
|
||||||
* Rust standard library is aware that types can implement `Default` and provides convenience methods that use it.
|
* Rust standard library is aware that types can implement `Default` and provides convenience methods that use it.
|
||||||
|
* the `..` syntax is called [struct update syntax][2]
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[1]: https://doc.rust-lang.org/std/default/trait.Default.html
|
[1]: https://doc.rust-lang.org/std/default/trait.Default.html
|
||||||
|
[2]: https://doc.rust-lang.org/book/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax
|
||||||
Reference in New Issue
Block a user