Skip to content

Commit 1b50385

Browse files
committed
deck binding is given cards vector demonstrating binding shorthand
1 parent 101a5e7 commit 1b50385

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

p01-deck/src/main.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,24 @@ fn main() {
3838

3939

4040
//in Rust, variables are referred to as 'bindings'
41+
//an empty vector can be created using a macro like vec![]
4142
//another equivalent way to create the empty vector would be Deck { cards: Vec::new() }
42-
let deck = Deck { cards: vec![] };
43+
// let deck = Deck { cards: vec![] };
44+
45+
//now that the cards vector exists it can be given to this struct literal instead of an empty vector
46+
// let deck = Deck { cards: cards };
47+
48+
//but the rust-analyzer gives a hint that the shorthand can be used here because the field and binding have identical names
49+
let deck = Deck { cards };
50+
4351

4452
//the :? inside the {} is the Debug formatter
4553
//which needs #[derive(Debug)] added to the struct Deck to make function without an error
4654
//but now it is able to print this struct to the console
47-
println!("Here's your deck: {:?}", deck);
55+
//adding the # to the formatter making it {:#?} makes it print in a 'pretty' way which is easier for a human to read
56+
println!("Here's your deck: {:#?}", deck);
4857

4958
// another way of expressing that formatted string is to put the binding name in the {}
50-
// println!("Here's your deck: {deck}");
59+
// println!("Here's your deck: {deck:#?}");
5160

52-
println!("Here's the cards vector: {:?}", cards)
5361
}

0 commit comments

Comments
 (0)