rustbook

Toy programs and snippets for The Rust Programming Language book
Log | Files | Refs

main.rs (1475B)


      1 enum IpAddrKind {
      2     V4,
      3     V6,
      4 }
      5 
      6 enum IpAddr {
      7     V4(u8, u8, u8, u8),
      8     V6(String),
      9 }
     10 
     11 fn route(ip_type: IpAddrKind) {}
     12 
     13 #[derive(Debug)]
     14 enum States {
     15     MA,
     16     RI,
     17     NY,
     18 }
     19 
     20 enum Coin {
     21     Penny,
     22     Nickel,
     23     Dime,
     24     Quarter(States),
     25 }
     26 
     27 fn coin_val(coin: Coin) -> u8 {
     28     match coin {
     29         Coin::Penny => 1,
     30         Coin::Nickel => 5,
     31         Coin::Dime => 10,
     32         Coin::Quarter(state) => {
     33             println!("State quarter from {:?}", state);
     34             25
     35         }
     36     }
     37 }
     38 
     39 fn plus_one(x: Option<u32>) -> Option<u32> {
     40     match x {
     41         Option::Some(i) => Some(i + 1),
     42         Option::None => None,
     43     }
     44 }
     45 
     46 fn main() {
     47     let four = IpAddrKind::V4;
     48     let six = IpAddrKind::V6;
     49     route(four);
     50     route(six);
     51 /*
     52     let home = IpAddr {
     53         kind: IpAddrKind::V4,
     54         address: String::from("127.0.0.1"),
     55     };
     56 
     57     let loopback = IpAddr {
     58         kind: IpAddrKind::V6,
     59         address: String::from("::1"),
     60     };
     61 */
     62 
     63     let home = IpAddr::V4(127, 0, 0, 1);
     64     let loopback = IpAddr::V6(String::from("::1"));
     65 /*
     66     let n: i8 = 10;
     67     let cat: Option<i8> = Some(5);
     68 //    let real = 
     69     let sum = cat + n;
     70 */
     71     println!("{}", coin_val(Coin::Quarter(States::MA)));
     72 
     73     let cat: Option<u32> = Some(10);
     74     let not: Option<u32> = None;
     75     println!("cat+1={:?}", plus_one(cat));
     76     println!("not+1={:?}", plus_one(not));
     77 
     78     let n: Option<u32> = None;
     79     if let None = n {
     80         println!("n has no value!");
     81     }
     82 }