Enter your book access code

The curriculum, drills, tutor, and interviewer mode are for readers of Ace the Rust Interview. The code is printed in the Preface (Companion site access).

Don’t have the book yet? The landing page stays open — purchase unlocks this gym.

beginner · order 1

The moved String

own-move-01Related book chapters: 1, 5 (optional — this page is complete on its own)ownershipmoveE0382

Slug: own-move-01 · Paired reading: Chapters 1 and 5

Prompt

Implement fn take_and_len(s: String) -> usize that returns the length of s. Then explain — in a comment in your solution, and aloud — why a caller cannot use s after passing it to take_and_len.

Predict first

Given let s = String::from("hi"); let n = take_and_len(s); println!("{s}"); — does it compile? Which error? What would go wrong at the end of the scope if it did?

Rust angle

Moves transfer ownership; the borrow checker rejects use-after-move so that exactly one owner runs Drop.