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

Added construction chapter #191

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
- [Enum Sizes](enums/sizes.md)
- [Methods](methods.md)
- [Method Receiver](methods/receiver.md)
- [Construction](structs/struct-construction.md)
- [Example](methods/example.md)
- [Pattern Matching](pattern-matching.md)
- [Destructuring Enums](pattern-matching/destructuring-enums.md)
Expand Down
8 changes: 8 additions & 0 deletions src/methods/receiver.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ are other possible receivers for a method:
Beyond variants on `self`, there are also
[special wrapper types](https://doc.rust-lang.org/reference/special-types-and-traits.html)
allowed to be receiver types, such as `Box<Self>`.

<details>

* In reality, all functions in Rust are static. Receivers just add the "dot syntax" syntactic sugar by implicitly passing the struct by move, reference, or mutable reference.

* You can explicitly call any of these methods with `StructName::method_name(&structure, /*the rest of the args*/)`

</details>
43 changes: 43 additions & 0 deletions src/structs/struct-construction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Construction

Unlike in C++ or Java, there is no "constructor" in Rust for structs, all
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to add this content — it's great for the course participants if we can cover constructors.

Please move the file to methods/constructors.md instead since constructors are not strictly about structs (they apply to enums too).

Then we just need to fix the small problem pointed out by Andrew and we should be good to go!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still like to see us cover this. Perhaps as part of @djmitche reorg in #1073.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will be in the methods slide?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think so too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I see it's mentioned on the slide about methods now.

construction is done using struct construction syntax.

It is convention, however, to either implement the `Default` trait, or create a
method called `new` that has no receiver (i.e., a "static" function).

```rust,editable
struct Person {
name: String,
age: u8,
}

impl Person {
pub fn new(name: String, age: u8) -> Person {
Person {
name,
age
}
}

pub fn new_birth(name: String) -> Person {
Self::new(name, 0)
}
}

fn main() {
let peter = Person::new(String::from("Peter"), 23);

println!("{} is {} years old", peter.name, peter.age);
}
```

<details>

* Mention the `Self` static scope accessor, it allows you to access any method of a struct.

* In fact, dot method call syntax is just syntactic sugar, you can even access methods with `&self` receiver parameters by explicitly passing structs in to the first parameter, e.g., `Person::display(&peter)` if it had such a method `display(&self)`.

* Mention it is likely better to take string references and clone them in the construction methods, but we wanted to keep the example simple and consistent with others.

</details>