mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-06-15 04:54:17 -06:00
ch5 methods almost complete
This commit is contained in:
parent
59bd34b9c8
commit
1f56ecfb24
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
|
@ -1 +1 @@
|
|||||||
{"rustc_fingerprint":13662911779824945272,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.83.0 (90b35a623 2024-11-26)\nbinary: rustc\ncommit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\ncommit-date: 2024-11-26\nhost: x86_64-pc-windows-msvc\nrelease: 1.83.0\nLLVM version: 19.1.1\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\nfmt_debug=\"full\"\noverflow_checks\npanic=\"unwind\"\nproc_macro\nrelocation_model=\"pic\"\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"lahfsahf\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_has_atomic_equal_alignment=\"128\"\ntarget_has_atomic_equal_alignment=\"16\"\ntarget_has_atomic_equal_alignment=\"32\"\ntarget_has_atomic_equal_alignment=\"64\"\ntarget_has_atomic_equal_alignment=\"8\"\ntarget_has_atomic_equal_alignment=\"ptr\"\ntarget_has_atomic_load_store\ntarget_has_atomic_load_store=\"128\"\ntarget_has_atomic_load_store=\"16\"\ntarget_has_atomic_load_store=\"32\"\ntarget_has_atomic_load_store=\"64\"\ntarget_has_atomic_load_store=\"8\"\ntarget_has_atomic_load_store=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_thread_local\ntarget_vendor=\"pc\"\nub_checks\nwindows\n","stderr":""},"12744816824612481171":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""}},"successes":{}}
|
{"rustc_fingerprint":13662911779824945272,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.83.0 (90b35a623 2024-11-26)\nbinary: rustc\ncommit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\ncommit-date: 2024-11-26\nhost: x86_64-pc-windows-msvc\nrelease: 1.83.0\nLLVM version: 19.1.1\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""}},"successes":{}}
|
@ -1 +1 @@
|
|||||||
{"rustc_fingerprint":13662911779824945272,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.83.0 (90b35a623 2024-11-26)\nbinary: rustc\ncommit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\ncommit-date: 2024-11-26\nhost: x86_64-pc-windows-msvc\nrelease: 1.83.0\nLLVM version: 19.1.1\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\nfmt_debug=\"full\"\noverflow_checks\npanic=\"unwind\"\nproc_macro\nrelocation_model=\"pic\"\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"lahfsahf\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_has_atomic_equal_alignment=\"128\"\ntarget_has_atomic_equal_alignment=\"16\"\ntarget_has_atomic_equal_alignment=\"32\"\ntarget_has_atomic_equal_alignment=\"64\"\ntarget_has_atomic_equal_alignment=\"8\"\ntarget_has_atomic_equal_alignment=\"ptr\"\ntarget_has_atomic_load_store\ntarget_has_atomic_load_store=\"128\"\ntarget_has_atomic_load_store=\"16\"\ntarget_has_atomic_load_store=\"32\"\ntarget_has_atomic_load_store=\"64\"\ntarget_has_atomic_load_store=\"8\"\ntarget_has_atomic_load_store=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_thread_local\ntarget_vendor=\"pc\"\nub_checks\nwindows\n","stderr":""},"12744816824612481171":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""}},"successes":{}}
|
{"rustc_fingerprint":13662911779824945272,"outputs":{"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\Brock\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"pc\"\nwindows\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.83.0 (90b35a623 2024-11-26)\nbinary: rustc\ncommit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\ncommit-date: 2024-11-26\nhost: x86_64-pc-windows-msvc\nrelease: 1.83.0\nLLVM version: 19.1.1\n","stderr":""}},"successes":{}}
|
7
rectangles/Cargo.lock
generated
Normal file
7
rectangles/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rectangles"
|
||||||
|
version = "0.1.0"
|
6
rectangles/Cargo.toml
Normal file
6
rectangles/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "rectangles"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
52
rectangles/src/main.rs
Normal file
52
rectangles/src/main.rs
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
fn main() {
|
||||||
|
// main_with_tuple();
|
||||||
|
main_with_struct();
|
||||||
|
// two values are unrelated in the code, only by name
|
||||||
|
// let length1 = 8;
|
||||||
|
// let width1 = 4;
|
||||||
|
|
||||||
|
// println!("The area of a rectangle is {} square units.",
|
||||||
|
// area(length1, width1)
|
||||||
|
// )
|
||||||
|
}
|
||||||
|
|
||||||
|
fn area (length: i32, width: i32) -> i32 {
|
||||||
|
length * width
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main_with_tuple () {
|
||||||
|
// vaules are related now but no distinction between length and width
|
||||||
|
// less clear, would have to remember that the first value is the length and second is width if used anywhere else such as drawing to the screen
|
||||||
|
let rectangle1 = (8, 4);
|
||||||
|
println!("The area of a rectangle is {} square units.",
|
||||||
|
area_with_tuple(rectangle1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// only needs one argument
|
||||||
|
fn area_with_tuple (dimensions: (i32, i32)) -> i32 {
|
||||||
|
// much less clear what is going on here, names only help a bit
|
||||||
|
dimensions.0 * dimensions.1
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Rectangle {
|
||||||
|
// negative length/width shapes dont exist
|
||||||
|
length: u32,
|
||||||
|
width: u32,
|
||||||
|
}
|
||||||
|
fn main_with_struct () {
|
||||||
|
// clearly defined what is going on without the need of comments
|
||||||
|
let rect1 = Rectangle {
|
||||||
|
length: 8,
|
||||||
|
width: 4,
|
||||||
|
};
|
||||||
|
println!("The area of a rectangle is {} square units.",
|
||||||
|
area_with_struct(&rect1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// prefer to borrow a struct rather than own
|
||||||
|
fn area_with_struct (rect: &Rectangle) -> u32 {
|
||||||
|
// way mroe clear the relationship between the two values
|
||||||
|
rect.length * rect.width
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user