add working with structs

This commit is contained in:
2026-10-05 23:05:14 +01:00
parent bdefae0637
commit ac9cc92228
7 changed files with 204 additions and 1 deletions
Generated
+4
View File
@@ -109,6 +109,10 @@ dependencies = [
"getrandom",
]
[[package]]
name = "structs"
version = "0.1.0"
[[package]]
name = "syn"
version = "2.0.119"
+1 -1
View File
@@ -1,5 +1,5 @@
[workspace]
members = ["common-programming-concepts", "common-programming-concepts-fibonacci", "common-programming-concepts-temp-converter", "common-programming-concepts-twelve-days-of-christmas","guessing-game", "ownership"]
members = ["common-programming-concepts", "common-programming-concepts-fibonacci", "common-programming-concepts-temp-converter", "common-programming-concepts-twelve-days-of-christmas","guessing-game", "ownership", "structs"]
resolver = "2"
[workspace.package]
+7
View File
@@ -0,0 +1,7 @@
[package]
name = "structs"
edition.workspace = true
version.workspace = true
license.workspace = true
[dependencies]
+78
View File
@@ -0,0 +1,78 @@
#![allow(dead_code)]
#![allow(unused_variables)]
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}
pub fn run() {
println!("\n=== defining and instantiating structs ===\n");
// data structures are roughly an object's data attributes, if comparing to OOP.
// it is a custom data type that allows you to group together multiple related values.
// In structs, we will name each piece of data . This is a bit like tuples, but the structs are
// more flexible and easier to access.
// The name of the struct should hold a meaningful name for all the pieces of data that are
// bundled together. Inside the struct, we have fields, which is each its own piece of the data
// structure.
// Here is an example of a user data structure.
let user1 = User {
active: true,
username: String::from("username123hello"),
email: String::from("someexample@example.com"),
sign_in_count: 1,
};
let uemail = user1.email;
println!("struct email: {uemail}");
let uemail = String::from("newemail@example.com");
println!("struct mut email: {uemail}");
let user2 = build_user(String::from("bartal.baek@example.com"), String::from("baraldur12345"));
let u2name = user2.username;
let u2email = user2.email;
println!("the name of user2 is {u2name}, and the email is {u2email}");
let update_user1 = User {
email: String::from("bxtal@example.com"),
..user1
};
// TUPLE STRUCTS
// groupings of related values, where the tuple is named, but the values are unnamed.
let black = Color(0,0,0);
let origin = Point(0,0,0);
let Point(x,y,z) = origin;
println!("the y point is {y} and the x point is {x}, and the z point is {z}");
// DEFINING UNIT-LIKE STRUCTS
// I do not really understand this, but it is creating an empty () struct.
// you do it like this
// struct AlwaysEqual;
// let subject = AlwaysEqual;
// OWNERSHIP OF STRUCT DATA
// Usage of lifetimes, in chap 10, no more explaination here.
}
fn build_user(email: String, username: String) -> User {
User {
active: true,
username,
email,
sign_in_count: 1,
}
}
+9
View File
@@ -0,0 +1,9 @@
mod defining_structs;
mod struct_program;
mod methods;
fn main() {
defining_structs::run();
struct_program::run();
methods::run();
}
+50
View File
@@ -0,0 +1,50 @@
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
fn width(&self) -> bool {
self.width > 0
}
fn can_hold(&self, other: &Rectangle) -> bool {
self.width > other.width && self.height > other.height
}
fn square(size: u32) -> Self {
Self {
width: size,
height: size,
}
}
}
pub fn run() {
println!("\n=== struct methods via impl ===");
let rect1 = Rectangle {
width: 30,
height: 50,
};
let rect2 = Rectangle {
width: 10,
height: 40,
};
let rect3 = Rectangle {
width: 60,
height: 45,
};
let sq = Rectangle::square(5);
println!(
"The area of the rectangle is {} square feet",
rect1.area()
);
println!("The rectangle has a nonzero width, it is {}", rect1.width());
println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
println!("this is a square\n{sq:#?}");
}
+55
View File
@@ -0,0 +1,55 @@
// Derive debug makes the struct printable for devs.
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
pub fn run() {
println!("\n=== structs program ===\n");
// program with just plain variables
let width1 = 30;
let height1 = 50;
println!(
"The are of the rectangle is {} square pixels",
area(width1,height1)
);
// program with struct tuple
let rect1 = (30,50);
println!(
"The tuple struct rectancle is {} square pixels",
tuple_struct_area(rect1)
);
// program with structs using the struct at the top of the script
let scale = 2;
let rect2 = Rectangle {
width: dbg!(30 * scale),
height: 50,
};
println!(
"The struct rectangle is {} square pixels",
struct_area(&rect2)
);
dbg!(&rect2);
println!("this is also debug for rect2\n{rect2:#?}");
println!("this is also debug but not as pretty with :? for rect2\n{rect2:?}");
}
fn area(width: u32, height: u32) -> u32 {
width * height
}
fn tuple_struct_area(dimensions: (u32,u32)) -> u32 {
dimensions.0 * dimensions.1
}
fn struct_area(rectangle: &Rectangle) -> u32 {
rectangle.width * rectangle.height
}