Today's tip: From Hello, World to hired.

Courses / Intermediate

Rust: Ownership without Tears

Ownership, borrowing and lifetimes, explained with pictures and fixed compiler errors, until the borrow checker feels like a friend.

9 lessons11 hours84k learnersCertificate with Pro

Start the free first lesson

Code you'll write

Borrowing and referencesrust
fn longest(words: &[String]) -> &str {
    let mut best = "";
    for w in words {
        if w.len() > best.len() {
            best = w;
        }
    }
    best
}
Error handling with Resultrust
use std::fs;

fn read_port(path: &str) -> Result<u16, Box<dyn std::error::Error>> {
    let text = fs::read_to_string(path)?;
    let port: u16 = text.trim().parse()?;
    Ok(port)
}