ch5 start
This commit is contained in:
parent
1008a76306
commit
42d7ad3555
7
ch5/defining_instantiating_structs/Cargo.lock
generated
Normal file
7
ch5/defining_instantiating_structs/Cargo.lock
generated
Normal file
@ -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"
|
6
ch5/defining_instantiating_structs/Cargo.toml
Normal file
6
ch5/defining_instantiating_structs/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "defining_instantiating_structs"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
53
ch5/defining_instantiating_structs/src/main.rs
Normal file
53
ch5/defining_instantiating_structs/src/main.rs
Normal file
@ -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,
|
||||||
|
}
|
||||||
|
}
|
7
ch5/example_structs/Cargo.lock
generated
Normal file
7
ch5/example_structs/Cargo.lock
generated
Normal file
@ -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"
|
6
ch5/example_structs/Cargo.toml
Normal file
6
ch5/example_structs/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "example_structs"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
23
ch5/example_structs/src/main.rs
Normal file
23
ch5/example_structs/src/main.rs
Normal file
@ -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
|
||||||
|
}
|
7
ch5/method_syntax/Cargo.lock
generated
Normal file
7
ch5/method_syntax/Cargo.lock
generated
Normal file
@ -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"
|
6
ch5/method_syntax/Cargo.toml
Normal file
6
ch5/method_syntax/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "method_syntax"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
38
ch5/method_syntax/src/main.rs
Normal file
38
ch5/method_syntax/src/main.rs
Normal file
@ -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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user