rustbook

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

main.rs (2274B)


      1 use std::cmp::PartialOrd;
      2 use std::fmt::Display;
      3 
      4 struct pt<T> {
      5     x: T,
      6     y: T,
      7 }
      8 
      9 //impl for any pt
     10 impl<T> pt<T> {
     11     fn x(&self) -> &T {
     12         &self.x
     13     }
     14 }
     15 
     16 //impl for only i32 pt
     17 impl pt<i32> {
     18     fn y(&self) -> &i32 {
     19         &self.y
     20     }
     21 }
     22 
     23 struct wack<T, U> {
     24     x: T,
     25     y: U,
     26 }
     27 
     28 //Blend wacks even with diferent types
     29 impl<T, U> wack<T, U> {
     30     fn blend<V, W>(self, other: wack<V, W>) -> wack<T, W> {
     31         wack {
     32             x: self.x,
     33             y: other.y,
     34          }
     35     }
     36 }
     37 
     38 
     39 enum blam<T, U> {
     40     Dwayne(T),
     41     Wigan(U),
     42 }
     43 
     44 pub trait Frobable {
     45 //    fn frob(&self) -> &str; //Just declare prototype
     46     fn frob(&self) -> &str {
     47         println!("This is the default if a type doesn't impl");
     48         "HELP!"
     49     }
     50 }
     51 
     52 pub struct Door {
     53     x: i32,
     54     y:i32,
     55     target:String,
     56 }
     57 
     58 impl Frobable for Door {
     59     fn frob(&self) -> &str {
     60         println!("Door target is {}", self.target);
     61         &self.target
     62     }
     63 }
     64 
     65 pub struct Switch {
     66     x: i32,
     67     y: i32,
     68     target:String,
     69 }
     70 
     71 impl Frobable for Switch {
     72     fn frob(&self) -> &str {
     73         println!("Switch target is {}", self.target);
     74         &self.target
     75     }
     76 }
     77 
     78 fn main() {
     79     let list1 = vec![34, 50, 25, 100, 65];
     80     let list2 = vec![66, 94, 200, 13, 123];
     81     let list3 = vec!['!', 'A', ']', '@', 'b'];
     82     max(&list1);
     83     max(&list2);
     84     max(&list3);
     85 
     86     let int_pt = pt{ x:1, y:4 };
     87     let float_pt = pt{ x: 1.2, y: 3.6 };
     88     float_pt.x;
     89     int_pt.x;
     90     let banana = wack{ x: 1, y: 1.0 };
     91     let mut who = blam::<i32, f64>::Dwayne(10);
     92     let who = blam::<i32, f64>::Wigan(1.1);
     93 
     94     let d = Door {
     95         x: 0,
     96         y: 0,
     97         target: "Over there".to_string(),
     98     };
     99     d.frob();
    100 
    101     let s = Switch {
    102         x: 0,
    103         y: 0,
    104         target: "Over there".to_string(),
    105     };
    106     s.frob();
    107 
    108 }
    109 
    110 //fn max(list: &[i32]) { //Allows any i32 slice
    111 //fn max(list: &Vec<i32>) { //Only Vectors
    112 //
    113 //Allows any vector of orderable, copyable, displayable types
    114 //fn max<T: Display + PartialOrd + Copy>(list: &Vec<T>) {
    115 //
    116 //Alt of the above
    117 fn max<T>(list: &Vec<T>) 
    118     where T: Display + PartialOrd + Copy,
    119 {
    120     let mut max = list[0];
    121 
    122     for &val in list {
    123         if val > max {
    124             max = val;
    125         }
    126     }
    127 
    128     println!("Max val={}", max);
    129 }