]>
git.proxmox.com Git - rustc.git/blob - src/doc/book/listings/ch13-functional-features/listing-13-01/src/main.rs
1 #[derive(Debug, PartialEq, Copy, Clone)]
8 shirts
: Vec
<ShirtColor
>,
12 fn giveaway(&self, user_preference
: Option
<ShirtColor
>) -> ShirtColor
{
13 user_preference
.unwrap_or_else(|| self.most_stocked())
16 fn most_stocked(&self) -> ShirtColor
{
20 for color
in &self.shirts
{
22 ShirtColor
::Red
=> num_red
+= 1,
23 ShirtColor
::Blue
=> num_blue
+= 1,
26 if num_red
> num_blue
{
35 let store
= Inventory
{
36 shirts
: vec
![ShirtColor
::Blue
, ShirtColor
::Red
, ShirtColor
::Blue
],
39 let user_pref1
= Some(ShirtColor
::Red
);
40 let giveaway1
= store
.giveaway(user_pref1
);
42 "The user with preference {:?} gets {:?}",
46 let user_pref2
= None
;
47 let giveaway2
= store
.giveaway(user_pref2
);
49 "The user with preference {:?} gets {:?}",