mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-07-06 11:07:12 -06:00
ch5 methods almost complete
This commit is contained in:
121
data_types.md
121
data_types.md
@ -453,4 +453,123 @@ struct User {
|
||||
}
|
||||
```
|
||||
|
||||
In short use data times that are owned rather than references
|
||||
In short use data times that are owned rather than references
|
||||
|
||||
## Adding Increased Functionality of Structs with derived traits
|
||||
|
||||
print can do many different types of formatting
|
||||
Cant print out structs by default because there are so many options
|
||||
with or without braces, commas, should all fields be shown
|
||||
This will cause an error
|
||||
```rust
|
||||
struct Rectangle {
|
||||
length: u32,
|
||||
width: u32,
|
||||
}
|
||||
let rect1 = Rectangle {
|
||||
length: 8,
|
||||
width: 4,
|
||||
};
|
||||
println!("rect1 contains {}", rect1);
|
||||
```
|
||||
|
||||
{} tell println to use Display by default because there is only one way to show many primitive data types
|
||||
|
||||
{var_name:?} this is for the format Debug
|
||||
{var_name:#?} this is for pretty printing in Debug format, good for larger structs
|
||||
|
||||
Debug is also not implemented for the struct and therefore not supported
|
||||
|
||||
```rust
|
||||
#[derive(Debug)]
|
||||
struct Rectangle {
|
||||
length: u32,
|
||||
width: u32,
|
||||
}
|
||||
// snip
|
||||
println!("rect1 contains {rect1:?}"); // single line print, in debug format, output: rect1 contains Rectangle { length: 8, width: 4 }
|
||||
println!("rect1 contains {rect1:#?}"); // pretty print in debug format, output: rect1 contains Rectangle {
|
||||
// length: 8,
|
||||
// width: 4,
|
||||
// }
|
||||
```
|
||||
|
||||
Another way to output pretty debug format by default is dbg! macro
|
||||
this prints out the file and line number as well of where it was called and returns the ownership of the value
|
||||
this prints to the stderr output stream
|
||||
this takes ownership of values compaired to println!
|
||||
prints to stdout output stream
|
||||
|
||||
example of using dbg
|
||||
```rust
|
||||
fn main() {
|
||||
let scale = 2;
|
||||
let rect1 = Rectangle {
|
||||
width: dbg!(30 * scale),
|
||||
height: 50,
|
||||
};
|
||||
dbg!(&rect1); // because it takes ownership need to pass in a reference
|
||||
}
|
||||
```
|
||||
output
|
||||
[src/main.rs:10:16] 30 * scale = 60
|
||||
[src/main.rs:14:5] &rect1 = Rectangle {
|
||||
width: 60,
|
||||
height: 50,
|
||||
}
|
||||
|
||||
## Methods
|
||||
Fucntions that are more closely related to structs
|
||||
|
||||
similar to functions decalred, param and output are all the same
|
||||
|
||||
run the code when the method is declared elsewhere
|
||||
|
||||
unlike functions they are defined in the context of a struct, an enum or a trait
|
||||
|
||||
first parameter is always self, which represents the instance of the struct that is is being called upon
|
||||
just like python methods
|
||||
|
||||
definition
|
||||
```rust
|
||||
struct Rectangle {
|
||||
length: u32,
|
||||
width: u32,
|
||||
}
|
||||
|
||||
// implementation block for Rectangle used to define fn related to the struct
|
||||
// put in this blcok so that the use case doesnt need to be searched
|
||||
impl Rectangle {
|
||||
// fn moved to here to that it has access to the instance with the self reference
|
||||
// fn now closely related to the rect struct
|
||||
// first param must be the type self: Self or &self which rust lets you shorthand
|
||||
// self can be borrowed, mutably borrowed, or take ownership of self
|
||||
// should always borrow unless need to transferownership or mutate the stored var
|
||||
// &mut self for mutable version of selfs
|
||||
// use self when you want to transform the self into something else
|
||||
fn area (&self) -> u32 {
|
||||
self.length * self.width
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
useage
|
||||
```rust
|
||||
let rect1 = Rectangle {
|
||||
length: 8,
|
||||
width: 4,
|
||||
}
|
||||
println!("The area of the reactangle is {} square units",
|
||||
// method syntax to call the area func
|
||||
// notice dont need any param in, already has access to self reference
|
||||
rect1.area()
|
||||
);
|
||||
```
|
||||
|
||||
this provides method syntax and dont have to repeat the structure they effect
|
||||
one impl can house all the methods for a struct, we can have tthe same method name as field name
|
||||
just differenitate with the use of () this is for a mthod not the field
|
||||
|
||||
this is used in getters where you want read only access to a struct, you can make the field pravate but the method public
|
||||
|
||||
130
|
Reference in New Issue
Block a user