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:
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
|
||||
}
|
Reference in New Issue
Block a user