diff --git a/ch5/defining_instantiating_structs/Cargo.lock b/ch5/defining_instantiating_structs/Cargo.lock new file mode 100644 index 0000000..6af51f0 --- /dev/null +++ b/ch5/defining_instantiating_structs/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "defining_instantiating_structs" +version = "0.1.0" diff --git a/ch5/defining_instantiating_structs/Cargo.toml b/ch5/defining_instantiating_structs/Cargo.toml new file mode 100644 index 0000000..a35cf9e --- /dev/null +++ b/ch5/defining_instantiating_structs/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "defining_instantiating_structs" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch5/defining_instantiating_structs/src/main.rs b/ch5/defining_instantiating_structs/src/main.rs new file mode 100644 index 0000000..560e119 --- /dev/null +++ b/ch5/defining_instantiating_structs/src/main.rs @@ -0,0 +1,53 @@ +struct User { + active: bool, + username: String, // using &str wont compile + email: String, // needs a lifetime which & doesn't do + sign_in_count: u64, +} + +// Just like haskell :) +#[derive(Debug)] +struct Color(i32, i32, i32); +#[derive(Debug)] +struct Point(i32, i32, i32); + +struct AlwaysEqual; + +fn main() { + let mut user1 = User { + email: String::from("email@mail.org"), + username: String::from("username"), + active: true, + sign_in_count: 1, + }; + user1.email = String::from("new_email@mail.org"); + + // update syntax + // borrows from user1, deallocating only the copied variables in the process. + // The variables not borrowed are left alone. Rust is cool + let user2 = User { + // active: user1.active, + // username: user1.username, + email: String::from("other_email@mail.org"), + // sign_in_count: user1.sign_in_count, + ..user1 // can do this for copying fields over on the shorthand + }; + + let black = Color(0, 0, 0); + let origin = Point(0, 0, 0); + + println!("black: {:?}", black); + println!("origin: {:?}", origin); + + // unit-like structs without any fields + let subject = AlwaysEqual; +} + +fn build_user(email: String, username: String) -> User { + User { + active: true, + username, // use a shorthand because this errors if not using a shorthand? + email, // That might have just been linting + sign_in_count: 1, + } +} diff --git a/ch5/example_structs/Cargo.lock b/ch5/example_structs/Cargo.lock new file mode 100644 index 0000000..756800f --- /dev/null +++ b/ch5/example_structs/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "example_structs" +version = "0.1.0" diff --git a/ch5/example_structs/Cargo.toml b/ch5/example_structs/Cargo.toml new file mode 100644 index 0000000..1bc9d7c --- /dev/null +++ b/ch5/example_structs/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "example_structs" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch5/example_structs/src/main.rs b/ch5/example_structs/src/main.rs new file mode 100644 index 0000000..4a84d0a --- /dev/null +++ b/ch5/example_structs/src/main.rs @@ -0,0 +1,23 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +fn main() { + let rect1 = Rectangle { + width: 30, + height: 50, + }; + + // prints to stdout + println!("Area of rect is: {:?} units", area(&rect1)); + println!("rect1 is: {rect1:#?}"); + + // prints to stderr + dbg!(&rect1); +} + +fn area(rect: &Rectangle) -> u32 { + rect.width * rect.height +} diff --git a/ch5/method_syntax/Cargo.lock b/ch5/method_syntax/Cargo.lock new file mode 100644 index 0000000..df4cc27 --- /dev/null +++ b/ch5/method_syntax/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "method_syntax" +version = "0.1.0" diff --git a/ch5/method_syntax/Cargo.toml b/ch5/method_syntax/Cargo.toml new file mode 100644 index 0000000..a2432bc --- /dev/null +++ b/ch5/method_syntax/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "method_syntax" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ch5/method_syntax/src/main.rs b/ch5/method_syntax/src/main.rs new file mode 100644 index 0000000..2d06392 --- /dev/null +++ b/ch5/method_syntax/src/main.rs @@ -0,0 +1,38 @@ +#[derive(Debug)] +struct Rectangle { + width: u32, + height: u32, +} + +// it is rare to see just "self" alone. +// Typically always see "&self" +// This is also very similar to Haskell +impl Rectangle { + // urhmm actkually these are *methods* and not *functions* + fn area(&self) -> u32 { + self.width * self.height + } + + fn width(&self) -> bool { + self.width > 0 + } +} + +fn main() { + let rect1 = Rectangle { + width: 30, + height: 50, + }; + + println!( + "The area of the rectangle is {} square pixels.", + rect1.area() + ); + + if rect1.width() { + println!("rect has a nonzero width; it is: {}", rect1.width); + } + + // stopped here: + // https://rust-book.cs.brown.edu/ch05-03-method-syntax.html#methods-and-ownership +}