1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
| #[derive(Debug)] enum IpAddrKind{ V4, V6, }
#[derive(Debug)] struct IpAddr { kind: IpAddrKind, address: String, }
#[derive(Debug)] enum Message{ Quit, Move {x: i32, y: i32}, Write(String), ChangeColor(i32, i32, i32), }
impl Message{ fn call(&self){ println!("Message: {:?}", self); } } #[derive(Debug)] enum Coin{ Penny, Nickel, Dime, Quarter, } #[derive(Debug)] enum CoinWithData{ Penny, Nickel, Dime, Quarter(UsState), } #[derive(Debug)] enum UsState{ Alaska, }
fn main() { let four = IpAddrKind::V4; let six = IpAddrKind::V6;
route(four); route(six); route(IpAddrKind::V4); route(IpAddrKind::V6);
let home = IpAddr { kind: IpAddrKind::V4, address: String::from("127.0.0.1"), }; println!("home: {:?}", home); println!("kind: {:?}", home.kind); println!("address: {:?}", home.address);
let q = Message::Quit; let m = Message::Move{x: 1, y: 2}; let w = Message::Write(String::from("hello")); let c = Message::ChangeColor(1, 2, 3); q.call(); m.call(); w.call(); c.call(); if let Message::Move{x, y} = m{ println!("x + y = {}", x + y); }
let some_number = Some(5); let some_string = Some("a string"); let absent_number: Option<i32> = None; println!("some_number: {:?}", some_number); println!("some_string: {:?}", some_string); println!("absent_number: {:?}", absent_number);
let six = plus_one(some_number); let none = plus_one(absent_number);
println!("six: {:?}", six); println!("none: {:?}", none);
let coin_1 = Coin::Penny; let coin_5 = Coin::Nickel; let coin_10 = Coin::Dime; let coin_25 = Coin::Quarter; println!("coin_1: {:?}", coin_1); println!("coin_5: {:?}", coin_5); println!("coin_10: {:?}", coin_10); println!("coin_25: {:?}", coin_25);
let datacoin_1 = CoinWithData::Penny; let datacon_5 = CoinWithData::Nickel; let datacoin_10 = CoinWithData::Dime; let datacoin_25 = CoinWithData::Quarter(UsState::Alaska);
println!("datacoin_1: {:?}", datacoin_1); println!("datacon_5: {:?}", datacon_5); println!("datacoin_10: {:?}", datacoin_10); println!("datacoin_25: {:?}", datacoin_25);
value_in_cents(coin_1); value_in_cents_binding_value(datacoin_25); let v = Some(7u8); match v { Some(1) => println!("one"), Some(3) => println!("three"), Some(5) => println!("five"), Some(7) => println!("seven"), _ => println!("anything"), };
if let Some(3) = v { println!("three"); }else{ println!("anything"); }
}
fn route(ip_kind: IpAddrKind) { println!("ip_kind: {:?}", ip_kind); }
fn value_in_cents(coin: Coin) -> u32{ match coin{ Coin::Penny => { println!("Lucky penny!"); 1 }, Coin::Nickel => 5, Coin::Dime => 10, Coin::Quarter => 25, } }
fn value_in_cents_binding_value(coin: CoinWithData) -> u32{ match coin{ CoinWithData::Penny => { println!("Lucky penny!"); 1 }, CoinWithData::Nickel => 5, CoinWithData::Dime => 10, CoinWithData::Quarter(state) => { println!("State quarter from {:?}!", state); 25 }, } }
fn plus_one(x: Option<i32>) -> Option<i32>{ match x{ None => None, Some(i) => Some(i + 1), } }
|