Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jul 17, 2024
1 parent f8043dd commit cb948fb
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 42 deletions.
8 changes: 4 additions & 4 deletions slides/rust/examples/struct/circural-references/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ fn main() {
let mut joe = Person {name: String::from("Joe"), partner: None};
#[allow(unused_mut)]
let mut jane = Person {name: String::from("Jane"), partner: None};
dbg!(&joe);
dbg!(&jane);
println!("{:?}", &joe);
println!("{:?}", &jane);
joe.partner = Some(&jane);
//jane.partner = Some(&joe);
dbg!(&joe);
dbg!(&jane);
println!("{:?}", &joe);
println!("{:?}", &jane);

}

Expand Down
4 changes: 2 additions & 2 deletions slides/rust/examples/struct/constructor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ fn main() {
name: String::from("Foo Bar"),
number: 42,
};
dbg!(sg);
println!("{:?}", sg);

let new = Something::qqrq();
dbg!(new);
println!("{:?}", new);
}
8 changes: 4 additions & 4 deletions slides/rust/examples/struct/debug-struct/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#[derive(Debug)]
struct Animal<'a> {
name: &'a str,
size: &'a str,
struct Animal {
name: String,
size: String,
weight: i32,
}

fn main() {
let eli = Animal {name: "elephant", size: "huge", weight: 100};
let eli = Animal {name: String::from("elephant"), size: String::from("huge"), weight: 100};
println!("{}", eli.name);
println!("{}", eli.size);
println!("{}", eli.weight);
Expand Down
12 changes: 6 additions & 6 deletions slides/rust/examples/struct/debugging-struct/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use std::fmt;

struct Animal<'a> {
name: &'a str,
size: &'a str,
struct Animal {
name: String,
size: String,
weight: i32,
}

impl std::fmt::Debug for Animal<'_> {
fn fmt(&self, format: &mut fmt::Formatter<'_>) -> fmt::Result {
impl std::fmt::Debug for Animal {
fn fmt(&self, format: &mut fmt::Formatter) -> fmt::Result {
write!(format, "Animal {{ name: {}, size: {}, weight: {} }}", self.name, self.size, self.weight)
}
}

fn main() {
let eli = Animal {name: "elephant", size: "huge", weight: 100};
let eli = Animal {name: String::from("elephant"), size: String::from("huge"), weight: 100};
println!("{}", eli.name);
println!("{}", eli.size);
println!("{}", eli.weight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ fn main() {
name: String::from("Foo Bar"),
input: Input { name: String::from("input text") },
};
dbg!(sg);
println!("{:?}", sg);

let new = Something::new();
dbg!(new);
println!("{:?}", new);

let empty = Something {
..Something::default()
};
dbg!(empty);
println!("{:?}", empty);


let with_name = Something {
name: String::from("Hello"),
..Something::default()
};
dbg!(with_name);
println!("{:?}", with_name);

}
8 changes: 4 additions & 4 deletions slides/rust/examples/struct/default/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ fn main() {
name: String::from("Foo Bar"),
number: 42,
};
dbg!(sg);
println!("{:?}", sg);

let empty = Something {
..Something::default()
};
dbg!(empty);
println!("{:?}", empty);

let with_name = Something {
name: String::from("Hello"),
..Something::default()
};
dbg!(with_name);
println!("{:?}", with_name);

let with_number = Something {
number: 42,
..Something::default()
};
dbg!(with_number);
println!("{:?}", with_number);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ struct Something {

fn main() {
let a = Something {number: 2, text: String::from("blue"), numbers: vec![5, 6]};
dbg!(&a);
println!("{:?}", &a);

let b = &a;
dbg!(&b);
dbg!(&a);
println!("{:?}", &b);
println!("{:?}", &a);
}

4 changes: 2 additions & 2 deletions slides/rust/examples/struct/new-method/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ fn main() {
name: String::from("Foo Bar"),
number: 42,
};
dbg!(sg);
println!("{:?}", sg);

let new = Something::new();
dbg!(new);
println!("{:?}", new);
}
3 changes: 3 additions & 0 deletions slides/rust/examples/struct/printing-struct-fails/out.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
elephant
huge
100
8 changes: 4 additions & 4 deletions slides/rust/examples/struct/printing-struct-fails/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
struct Animal<'a> {
name: &'a str,
size: &'a str,
struct Animal {
name: String,
size: String,
weight: i32,
}

fn main() {
let eli = Animal {name: "elephant", size: "huge", weight: 100};
let eli = Animal {name: String::from("elephant"), size: String::from("huge"), weight: 100};
println!("{}", eli.name);
println!("{}", eli.size);
println!("{}", eli.weight);
Expand Down
12 changes: 6 additions & 6 deletions slides/rust/examples/struct/printing-struct/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use std::fmt;

struct Animal<'a> {
name: &'a str,
size: &'a str,
struct Animal {
name: String,
size: String,
weight: i32,
}

impl std::fmt::Display for Animal<'_> {
fn fmt(&self, format: &mut fmt::Formatter<'_>) -> fmt::Result {
impl std::fmt::Display for Animal {
fn fmt(&self, format: &mut fmt::Formatter) -> fmt::Result {
write!(format, "name: {}, size: {}, weight: {}", self.name, self.size, self.weight)
}
}


fn main() {
let eli = Animal {name: "elephant", size: "huge", weight: 100};
let eli = Animal {name: String::from("elephant"), size: String::from("huge"), weight: 100};
println!("{}", eli.name);
println!("{}", eli.size);
println!("{}", eli.weight);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
struct Course<'a> {
name: &'a str,
struct Course {
name: String,
grades: Vec<i32>,
final_grade: Option<i32>,
}

fn main() {
let mut c = Course { name: "Programming Rust", grades: vec![78, 80], final_grade: None };
let mut c = Course { name: String::from("Programming Rust"), grades: vec![78, 80], final_grade: None };
println!("{}", c.name);
println!("{:?}", c.grades);
println!("{:?}", c.final_grade);
Expand Down
1 change: 1 addition & 0 deletions slides/rust/struct.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
* We can print the values of the individual attributes of a struct, but we cannot print the whole struct.

![](examples/struct/printing-struct-fails/src/main.rs)
![](examples/struct/printing-struct-fails/out.out)

## Print struct
{id: print-struct}
Expand Down

0 comments on commit cb948fb

Please sign in to comment.