The fn keyword (pronounced “fun”) introduces a function.

The mut keyword (pronounced “mute) is short for mutable.

A &str (pronounced “stir” or “string slice”) is a reference to a run of UTF-8 text owned by someone else: it “borrows” the text.

A value of type &String (pronounced “ref String”) is a reference to a String value.

The expression &e yields a shared reference to e’s value; if e has the type T, then &e has the type &T, pronounced “ref T.”

The expression &mut e yields a mutable reference to e’s value; you write its type as &mut T, which is pronounced “ref mute T.”

Read the <T> after the type name as “for any type T.”

1
2
3
4
5
6
struct Complex<T> {
    /// Real portion of the complex number
    re: T,
    /// Imaginary portion of the complex number
    im: T,
}

Read the <T: FromStr> as “For any type T that implements the FromStr trait…”.

1
fn parse_pair<T: FromStr>(s: &str, separator: char) -> Option<(T, T)> {}

The lifetime 'a (pronounced “tick A”) is a lifetime parameter of f. Read <'a> as “for any lifetime 'a”. So when we write fn f<'a>(p: &'a i32), we’re defining a function that takes a reference to an i32 with any given lifetime 'a.

1
2
3
4
5
6
static mut STASH: &i32 = &128;
fn f<'a>(p: &'a i32) {
    unsafe {
        STASH = p;
    }
}

Read the <T> in Queue<T> as “for any element type T…”. So this definition reads, “For any type T, a Queue<T> is two fields of type Vec<T>.”

1
2
3
4
pub struct Queue<T> {
    older: Vec<T>,
    younger: Vec<T>
}

Read the line impl<T> Queue<T> as something like, “for any type T, here are some associated functions available on Queue<T>.” Then, you can use the type parameter T as a type in the associated function definitions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
impl<T> Queue<T> {
    pub fn new() -> Queue<T> {
        Queue { older: Vec::new(), younger: Vec::new() }
    }

    pub fn push(&mut self, t: T) {
        self.younger.push(t);
    }

    pub fn is_empty(&self) -> bool {
        self.older.is_empty() && self.younger.is_empty()
    }
}