rustbook

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

main.rs (3083B)


      1 use std::collections::HashMap;
      2 
      3 fn print_all_elements(v: &Vec<i32>) {
      4     for i in v {
      5         println!("{}", i);
      6     }
      7 }
      8 
      9 fn beef_all_elements(v: &mut Vec<i32>) {
     10     for i in v {
     11         *i += 50;
     12         println!("{}", i);
     13     }
     14 }
     15 
     16 enum Cell {
     17     Int(i32),
     18     Float(f64),
     19     Text(String),
     20 }
     21 
     22 fn main() {
     23     let mut v: Vec<i32> = Vec::new();
     24     v.push(10);
     25     v.push(20);
     26     v.push(30);
     27     v.push(40);
     28 
     29     let mut v2 = vec![1,2,3];
     30     v2.push(4);
     31     v2.push(5);
     32     v2.push(6);
     33     v2.push(7);
     34 
     35     println!("v2[2]={}", &v2[2]);
     36     println!("v2[3]={}", &v2[3]);
     37     println!("v2[4]={}", &v2[4]);
     38 //    let does_net_exist = v2[100]); //Crash
     39 //    let does_net_exist = v2.get(100);
     40 
     41 //    let first = &v2[0];
     42     v2.push(8);
     43 //    println!("first={}", first); Compile error firist is made invalid after push
     44 
     45     match v.get(2) {
     46         Some(val) => println!("val={}", val),
     47         None => println!("No entry at index"),
     48     }
     49 
     50     print_all_elements(&v2);
     51     beef_all_elements(&mut v2);
     52 
     53     let mut row: Vec<Cell> = Vec::new();
     54     row.push(Cell::Int(10));
     55     row.push(Cell::Float(12.6));
     56     row.push(Cell::Text(String::from("Frog")));
     57 
     58     
     59 //   let mut s  = String::new();
     60     let data = "Inital Contents";
     61     let mut s = data.to_string();
     62 
     63     s.push_str(", some more content ");
     64 
     65     let s2 = "Make me a string".to_string();
     66 
     67     s.push_str(&s2);
     68     s.push('!');
     69     println!("{}", s);
     70     println!("{}", s2);
     71 
     72     s = s + &s2;
     73     println!("{}", s);
     74 
     75     let tic = "tic".to_string();
     76     let tac = "tac".to_string();
     77     let toe = "toe".to_string();
     78 
     79     let ttt = format!("{}-{}-{}", tic, tac, toe); //Doesn't take ownerhship
     80     let ttt2 = tic + "-" + &tac + "-" + &toe; //Takes owenership of tic
     81     println!("{}", ttt);
     82     println!("{}", ttt2);
     83 
     84 
     85     let a = &ttt[5..6];
     86     println!("{}", a);
     87 
     88     for c in ttt.chars() {
     89         println!("{}", c);
     90     }
     91 
     92     for b in ttt.bytes() {
     93         println!("{}", b);
     94     }
     95 
     96     let mut scores = HashMap::new();
     97 
     98     scores.insert(String::from("Red"), 10);
     99     scores.insert(String::from("Blue"), 15);
    100 
    101     let teams = vec!["Blue".to_string(), "Red".to_string(), "Yellow".to_string(), "Green".to_string()];
    102     let starting_points = vec![10, 15, 3, 24];
    103 
    104     let mut points: HashMap<String, i32> = teams.into_iter().zip(starting_points.into_iter()).collect();
    105 
    106     let red_score = points.get(&"Red".to_string());
    107     if let Some(_) = red_score {
    108         println!("red score: {:?}", red_score);
    109     }
    110 
    111     points.insert("Red".to_string(), 1000);
    112     points.entry("Red".to_string()).or_insert(2000);
    113     points.entry("Orange".to_string()).or_insert(2000);
    114 
    115     for (key, value) in points {
    116         println!("{} | {}", key, value);
    117     }
    118 
    119     let text = "green ball red ball yellow ball blue ball";
    120     let mut wordcount = HashMap::new();
    121 
    122     for word in text.split_whitespace() {
    123         // if word doesn't exist set it to 0
    124         // if word does exist set c to be a reference to the value at word
    125         let c = wordcount.entry(word).or_insert(0);
    126         *c += 1;
    127     }
    128     println!("{:?}", wordcount);
    129 }