-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e29dbe8
Added construction chapter
5ce09aa
added a note to another page
fcda3ee
Update src/methods/receiver.md
mgeisler 0122f34
Update src/structs/struct-construction.md
mgeisler af3fefb
Update src/structs/struct-construction.md
mgeisler c2f177e
Update src/structs/struct-construction.md
mgeisler ecffee8
Update src/structs/struct-construction.md
mgeisler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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 (ie a "static" function). | ||
mgeisler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```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, eg `Person::display(&peter)` if it had such a method `display(&self)`. | ||
mgeisler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
* 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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.