mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-07-06 11:07:12 -06:00
onto struct ex
This commit is contained in:
@ -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
|
Reference in New Issue
Block a user