From e29dbe848efd10c27aab85481fdae540f1e2085a Mon Sep 17 00:00:00 2001 From: Brandon Pollack Date: Fri, 20 Jan 2023 14:56:28 +0900 Subject: [PATCH 1/7] Added construction chapter --- src/SUMMARY.md | 1 + src/structs/struct-construction.md | 43 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/structs/struct-construction.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index e42dbf56f1bb..1fff51bcf794 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) diff --git a/src/structs/struct-construction.md b/src/structs/struct-construction.md new file mode 100644 index 000000000000..0f017e200855 --- /dev/null +++ b/src/structs/struct-construction.md @@ -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). + +```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); +} +``` + +
+ +* 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)`. + +* 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. + +
\ No newline at end of file From 5ce09aa20eadd329642db401d3ea3ddb8023431f Mon Sep 17 00:00:00 2001 From: Brandon Pollack Date: Fri, 20 Jan 2023 14:59:49 +0900 Subject: [PATCH 2/7] added a note to another page --- src/methods/receiver.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/methods/receiver.md b/src/methods/receiver.md index 4c3f7e513a0a..4b75dbb4d865 100644 --- a/src/methods/receiver.md +++ b/src/methods/receiver.md @@ -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`. + +
+ +* 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*/)` + +
\ No newline at end of file From fcda3ee199e8a5d7e754beeb2e1770348047343e Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Fri, 20 Jan 2023 11:29:46 +0100 Subject: [PATCH 3/7] Update src/methods/receiver.md --- src/methods/receiver.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/methods/receiver.md b/src/methods/receiver.md index 4b75dbb4d865..9bed3eb04ae1 100644 --- a/src/methods/receiver.md +++ b/src/methods/receiver.md @@ -20,7 +20,7 @@ allowed to be receiver types, such as `Box`.
-* 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. +* 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*/)` From 0122f342dbb11c6eeb85db317ac05ebd223175eb Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Fri, 20 Jan 2023 11:30:31 +0100 Subject: [PATCH 4/7] Update src/structs/struct-construction.md --- src/structs/struct-construction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structs/struct-construction.md b/src/structs/struct-construction.md index 0f017e200855..69109b225d43 100644 --- a/src/structs/struct-construction.md +++ b/src/structs/struct-construction.md @@ -3,7 +3,7 @@ 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 +It is convention, however, to either implement the `Default` trait, or create a method called "new" that has no receiver (ie a "static" function). ```rust,editable From af3fefb94df69f362b7cb38238713c801a6d432e Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Fri, 20 Jan 2023 11:30:37 +0100 Subject: [PATCH 5/7] Update src/structs/struct-construction.md --- src/structs/struct-construction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structs/struct-construction.md b/src/structs/struct-construction.md index 69109b225d43..5e9046debdda 100644 --- a/src/structs/struct-construction.md +++ b/src/structs/struct-construction.md @@ -1,6 +1,6 @@ # Construction -Unlike in C++ or Java, there is no "constructor" in rust for structs, all +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 From c2f177eccb62b0a9435b51af5455c797e323ebca Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Fri, 20 Jan 2023 11:31:02 +0100 Subject: [PATCH 6/7] Update src/structs/struct-construction.md --- src/structs/struct-construction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structs/struct-construction.md b/src/structs/struct-construction.md index 5e9046debdda..7c61f4cc880a 100644 --- a/src/structs/struct-construction.md +++ b/src/structs/struct-construction.md @@ -4,7 +4,7 @@ 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). +method called `new` that has no receiver (i.e., a "static" function). ```rust,editable struct Person { From ecffee836d07271e856cf43976c5e73712626779 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Fri, 20 Jan 2023 11:31:34 +0100 Subject: [PATCH 7/7] Update src/structs/struct-construction.md --- src/structs/struct-construction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structs/struct-construction.md b/src/structs/struct-construction.md index 7c61f4cc880a..5d8c3bf2dcfd 100644 --- a/src/structs/struct-construction.md +++ b/src/structs/struct-construction.md @@ -36,7 +36,7 @@ fn main() { * 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)`. +* 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.