Compare commits

...

3 Commits

Author SHA1 Message Date
Tyrel Souza b5dec65244
more struct methods, receiving another struct 2023-02-27 00:13:07 -05:00
Tyrel Souza 92b1c38688
struct with fields 2023-02-26 23:51:24 -05:00
Tyrel Souza 099a55d642
tuple structs 2023-02-26 23:49:38 -05:00
4 changed files with 93 additions and 0 deletions

45
rectangles/.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,45 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'rectangles'",
"cargo": {
"args": [
"build",
"--bin=rectangles",
"--package=rectangles"
],
"filter": {
"name": "rectangles",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'rectangles'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=rectangles",
"--package=rectangles"
],
"filter": {
"name": "rectangles",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

7
rectangles/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "rectangles"
version = "0.1.0"

8
rectangles/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "rectangles"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

33
rectangles/src/main.rs Normal file
View File

@ -0,0 +1,33 @@
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
fn can_hold(&self, r2: &Rectangle) -> bool {
self.width > r2.width && self.height > r2.height
}
}
// MAIN
fn main() {
let rect1 = Rectangle { width:30, height:50};
let rect2 = Rectangle { width:10, height:40};
let rect3 = Rectangle { width:60, height:45};
println!("r: {}", rect1.area());
println!("r: {}", rect2.area());
println!("r: {}", rect3.area());
println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
}