started ch5

This commit is contained in:
2025-01-03 14:33:03 -07:00
parent 2e3f133c61
commit cec6c130ac
3 changed files with 112 additions and 2 deletions

View File

@ -309,4 +309,114 @@ String concatinization example
let mut s = String::from("hello");
s.push_str(", world!"); // push_str() appends a literal to a String
println!("{s}"); // This will print `hello, world!`
```
# Structures
Custom data type that packages up multiple data types into a meaningful manner and call the collection something
More similar to an object, can define related methods to them
Similar to tuples but have to name and define everything inside a struct, like a dictionary but with set names and order
Dont need to know order just know key
## Defining
Need ``struct`` keywork then name of struct, which should describe the significance of the gropued data
All values are seperated by commas these are called fields
general definition of the type created
```rust
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}
```
## Initiating
To use give a owning var name thne concretely define what each value is
define the key: vaule pairs
dont need to initate in the same order they were defined in
```rust
let mut user1 = User {
active: true,
username: String::from("someusername123"),
email: String::from("someone@example.com"),
sign_in_count: 1,
};
```
to access values from the struct the ``dot``notation is used
note the WHOLE struct must be mutable, rust does not allow for partial mutability
```rust
// user1 email field now is equal to the string example@mail.com
user1.email = String::from("example@mail.com");
```
Can build a struct with implicit values input by default
```rust
fn build_user (email: String, username: String) {
User {
active: true,
email: email,
username: username,
sign_in_count: 1,
}
}
```
### Init Field Shorthand
This is useful when the param and the struct definition share the same name.
This reduces the amount of time spent on repeating key:value pairs
```rust
fn build_user(email: String, username: String) -> User {
User {
active: true,
username,
email,
sign_in_count: 1,
}
}
```
Only works beacuse param share same name as field key
this is equivalent to ```username:username or email:email```
### Creating Instances from Other Instances with Struct Update Syntax
Often useful to do so, only need to change 1 value
Slow method
```rust
let user2 = User {
active: user1.active,
username: user1.username,
email: String::from("another@emial.com"),
sign_in_count: user.sign_in_count,
};
```
Using Update syntax ``..`` this can be done a LOT Quicker
```rust
let user2 = User {
email: String::from("another@email.com"),
..user1
}
// user1 no longer completely valid
// can still use user1.email, .active and .sign_in_count
```
This specifies htat the fields not explicity set should be the same as the given instance
This uses the = assignment operator and therefore a ownership move occurs with the ../update syntax
user1 would still be valid if both of the String types in were given new values
## Tuple Structs
This is also allowed but not key:value pairs
This still holds values in the field
```rust
struct RGBColour (i32, i32, i32);
struct Point (i32, i32, i32);
let black = RGBColour(0, 0, 0);
let origin = Point(0, 0, 0);
```