Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[idioms/ctor] adds default constructor section #280

Merged
Prev Previous commit
Next Next commit
fixup! [idioms/ctor] adds default constructor section
links to *associated function* to the book
  • Loading branch information
wookietreiber committed Nov 20, 2021
commit 1fe8f7a61d5efacd1bd5d475d219277e3cae3e7c
5 changes: 3 additions & 2 deletions idioms/ctor.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Description

Rust does not have constructors as a language construct. Instead, the
convention is to use an associated `new` function to create an object:
convention is to use an [associated function][] `new` to create an object:

```rust
/// Time in seconds.
Expand Down Expand Up @@ -68,7 +68,7 @@ pub struct Second {
```

**Note:** When implementing `Default` for a type, it is neither required nor
recommended to also provide an associated `new` function without arguments.
recommended to also provide an associated function `new` without arguments.

**Hint:** The advantage of implementing or deriving `Default` is that your type
can now be used where a `Default` implementation is required, most prominently,
Expand All @@ -82,5 +82,6 @@ any of the [`*or_default` functions in the standard library][std-or-default].
- The [builder pattern](../patterns/creational/builder.md) for constructing
objects where there are multiple configurations.

[associated function]: https://doc.rust-lang.org/stable/book/ch05-03-method-syntax.html#associated-functions
[std-default]: https://doc.rust-lang.org/stable/std/default/trait.Default.html
[std-or-default]: https://doc.rust-lang.org/stable/std/?search=or_default