<< Previous | Next >>

Daily Learnings: Wed, Nov 08, 2023

You won’t skid if you stay in a rut. — Kin Hubbard

Rust Study

I was able to sneak in a bit more Rust study today, continuing to chip away at the massive, 14hr intro course found on YouTube.

Partial Moves

Within the destructuring of a single variable, both by-move and by-reference pattern binding can be used at the same time. Doing this will result in a “partial move” of the variable, which means that parts of the variable will be moved while other parts stay. In such a case, the parent variable cannot be used afterwards as a whole; however, the parts that are only referenced (and not moved) can still be used

Essentially, this means that if a parent variable of some sort is destructured into other variables, the parent as a whole cannot be referenced anymore, but children values can if they weren’t moved to others.

fn main() {
    let t: (String, String) = (String::from("hello"), String::from("world"));
    // NOTE: This works just fine, as no destructuring has occurred yet
    println!("{:?}", t);

    let _s: String = t.0;

    // The following throws an error
    // println!("{:?}", t);

    // But this works just fine
    println!("{:?}", t.1);
}

References