10.7 Slicing Methods: split_at, chunks, windows

Right, let’s talk about slicing up your data. You’ve got a big, contiguous array of stuff, and you need to work with smaller, more manageable pieces of it. This is where split_at, chunks, and windows come in. They’re your go-to tools for non-allocating, view-into-your-data operations. They don’t copy the data; they just give you different lenses to look at it through. It’s efficient, and it’s the kind of zero-cost abstraction Rust is famous for.

10.6 String Literals as &'static str

Let’s talk about one of the most comforting little lies Rust tells you: "hello world". You write it, the compiler accepts it, and you think you’ve created a string. But you haven’t. Not a String, anyway. What you’ve actually created is a string literal, and its type is &'static str. This is one of the most important and misunderstood types in Rust, so let’s pull it apart. The Anatomy of &'static str The signature &'static str is a concentrated dose of Rust’s philosophy. Let’s read it from right to left:

10.5 Fat Pointers: How Slices Carry Length Without Allocation

Alright, let’s talk about one of the most brilliantly simple yet initially confusing concepts in Rust: the slice. Specifically, we’re going to dissect how they manage to know their own length without being some special, heap-allocated monstrosity. The answer is a concept called a “fat pointer,” and it’s one of my favorite pieces of Rust’s design. It’s so obvious in hindsight you’ll wonder why more languages don’t do it. Think about a simple reference, like &i32. It’s a thin pointer. Under the hood, on a 64-bit system, it’s just a single 8-byte address pointing to some integer living elsewhere in memory. It has no idea what’s around it. It’s like knowing the exact street address of a single house but having no clue how long the street is. This is fine, until you need to talk about a whole contiguous range of houses.

10.4 Slice Indexing and Range Syntax

Right, let’s talk about slicing the slice. It’s a bit meta, but it’s also where you’ll spend a lot of your time and, consequently, where you’ll meet the dreaded panic! if you’re not careful. I’m going to show you how to avoid that fate. Think of a slice as a window into some data. Indexing is how you point to a specific seat in the row of data your window is looking at. You use the index operator, [], with a single usize value.

10.3 &[T]: Array and Vec Slice

Right, let’s talk about slices. You’ve met arrays ([T; N]) and you’ve met vectors (Vec<T>). They’re great. They own their data. But what if you don’t want ownership? What if you just want to borrow a view into a contiguous sequence of elements, be it from an array on the stack or a vector on the heap? You don’t want to copy the data; you just want to look at it, or maybe tell someone else where to look. That, my friend, is the slice type: &[T].

10.2 &str: A String Slice (Pointer + Length)

Right, let’s talk about &str. You’ve probably met its more demanding, heap-allocated cousin, String. String is the one that’s always asking for more—more memory, more capacity, more responsibility. &str is the chill, minimalist friend. It doesn’t own anything. It’s a guest, a spectator, a borrowed view. Specifically, it’s a string slice, and it’s one of the most brilliantly designed parts of Rust. Think of a &str as a data-scientist’s perfect pointer: it’s not just a memory address. It’s a two-part value:

10.1 Slices as References to a Contiguous Sequence

Right, so we’ve been dealing with ownership and Strings, which is a bit like being handed the title to a car. It’s yours. You drive it, you wreck it, you’re responsible for its scrap metal. But what if you just need to borrow the car for a quick trip to the store? You don’t want the full responsibility of ownership; you just want a specific, contiguous part of it for a little while.

— joke —

...