onto struct ex

This commit is contained in:
2025-01-05 17:35:31 -07:00
parent cec6c130ac
commit 59bd34b9c8
4 changed files with 39 additions and 4 deletions

View File

@ -419,4 +419,38 @@ This still holds values in the field
let black = RGBColour(0, 0, 0);
let origin = Point(0, 0, 0);
```
```
## Unit Like Struct
This is similar to a unit ()
This holds no data in itself
Useful for when you need to implement a trait on some type but dont want to store data in the type itself
Delcaration
```rust
struct unit_like_type;
let using_unit_like_struct = unit_like_type; // instance of unit_like_type
```
No need for () in the delcaration
## Structure Ownership
Want each instance of a struct to own the values inside so that the values inside are always valid unless specified
Can use references but need to take advantage of lifetimes which ensures that the reference is valid whilst the structure is valid
This is valid but compiler will ask for lifetime specifiers
```rust
struct User {
active: bool,
username: &str,
email: &str,
sign_in_count: u64,
}
```
In short use data times that are owned rather than references