Skip to content

Latest commit

 

History

History
197 lines (151 loc) · 3.67 KB

how-to-learn-Rust_Tim-McNamara_2021.md

File metadata and controls

197 lines (151 loc) · 3.67 KB

Video talk

  • Tim's beliefs:

  • Where should I start?

fn main() {
    println!("Hallo, Linz");
}
  • How do I use a variable?
fn main() {
    let name = "Linz";

    println!("Hallo, {}!", name);
}
  • How do I create my own type?
struct Greeting {
    name: String
}

fn main() {
    let greeting = Greeting {name: "Linz".to_string()};

    println!("Hallo, {}!", greeting.name);
}
  • What is a constructor?
struct Greeting {
    name: String,
}

impl Greeting {
    fn new(name: &str) -> Self {
        Greeting {
            name: name.to_string(),
        }
    }
}

fn main() {
    let greeting = Greeting::new("Linz");

    println!("Hallo, {}!", greeting.name);
}
  • How do I accept a &str or a String ?
struct Greeting {
    name: String,
}

impl Greeting {
    fn new<T: AsRef<str>>(name: T) -> Self {
        Greeting {
            name: name.as_ref().to_string(),
        }
    }
}

fn main() {
    let greeting = Greeting::new("Linz");

    println!("Hallo, {}!", greeting.name);
}
  • How can I ask a Greeting to print itself ?
use std::fmt;

struct Greeting {
    name: String,
}

impl Greeting {
    fn new<T: AsRef<str>>(name: T) -> Self {
        Greeting {
            name: name.as_ref().to_string(),
        }
    }
}

impl fmt::Display for Greeting {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Hallo, {}!", self.name)
    }
}

fn main() {
    let greeting = Greeting::new("Linz");

    println!("{}", greeting);
}
  • Rust can feel magical, it is common to be confused
// std::mem::drop

// The underscore annotation signals that the argument might not be used
pub fn drop<T>(_x: T) {}
use std::collections::HashMap;

let mut letters = HashMap::new();

for ch in "some text".chars() {
    // 'counter' is a mutable reference for what it is inside of the HashMap
    let counter = letters.entry(ch).or_insert(0);

    // dereference it
    *counter += 1;
}
// std::convert::Intro;

impl<T, U> Into<U> for T
where U: From<T>, {
    fn into(self) -> U {
        U::from(self)
    }
}
  • Give yourself permission to use .clone() and .to_string(). Memory allocations only require dozens of nanoseconds.
  • There is more than one way to achieve a particular outcome:
fn main() {
    let needle = 0o204;
    let haystack = vec![1, 2, 5, 132, 429, 1430];

    for item in &haystack {
        if *item == needle {
            println!("{}", item);
            break;
        }
    }

    // or
    if haystack.contains(&needle) {
        println!("{}", needle);
    }
}
fn main() {
    let penguin_data = "\
    common name, length (cm)
    Little penguin, 33
    YellowEyed penguin, 65
    Fiord land penguin, 60
    Invalid, data
    ";

    let records = penguin_data.lines();

    for record in records {
        let fields: Vec<_> = record
            .split(',')
            .map(|field| field.trim())
            .collect();

        let name = fields[0];
        let height: Result<f32, _> = fields[1].parse();
        if height.is_err() {
            continue;
        }

        println!("{} has {} cm", name, height.unwrap());
    }
}