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

Add reference to lifetime in structs #1274

Merged
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
10 changes: 6 additions & 4 deletions src/custom_types/structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ There are three types of structures ("structs") that can be created using the
```rust,editable
#[derive(Debug)]
struct Person<'a> {
// The 'a defines a lifetime
name: &'a str,
age: u8,
}
Expand Down Expand Up @@ -54,7 +55,7 @@ fn main() {
// Make a new point by using struct update syntax to use the fields of our
// other one
let bottom_right = Point { x: 5.2, ..point };

// `bottom_right.y` will be the same as `point.y` because we used that field
// from `point`
println!("second point: ({}, {})", bottom_right.x, bottom_right.y);
Expand Down Expand Up @@ -86,14 +87,15 @@ fn main() {

### Activity

1. Add a function `rect_area` which calculates the area of a rectangle (try
using nested destructuring).
1. Add a function `rect_area` which calculates the area of a rectangle (try
using nested destructuring).
2. Add a function `square` which takes a `Point` and a `f32` as arguments, and returns a `Rectangle` with its lower left corner on the point, and a width and height corresponding to the `f32`.

### See also:

[`attributes`][attributes] and [destructuring][destructuring]
[`attributes`][attributes], [lifetime][lifetime] and [destructuring][destructuring]

[attributes]: ../attribute.md
[c_struct]: https://en.wikipedia.org/wiki/Struct_(C_programming_language)
[destructuring]: ../flow_control/match/destructuring.md
[lifetime]: ../scope/lifetime.md