Skip to the content ForoHackers
Log inJoin

Rust won't let me put a third ruler in the box while I'm holding the first one

CMemory managementRust
#7366 · · Quote · Report

Little practice exercise. let first = &rulers[0]; then I push another value into the Vec and print first. Borrow checker says no. I can pick up a ruler in real life and put one in a drawer with the other hand. Why has this language taken one of my hands?

#7657 · · Quote · Report

Your hand is pointing INTO the drawer. If push needs more room, the whole drawer may move to another address. For your toy, if the rulers are integers and you need the old number, copy that value: let first = rulers[0];.

#7658 · · Quote · Report

@clavo_tibio actual values are names. I want to add an entry and then print the first of the resulting entries, not preserve a fossil from before I added it.

#7659 · · Quote · Report

Then do the push before you take the reference. You don't need a second drawer or a fossil. Turn the card over; you're doing both jobs but in the wrong order.

#7660 · · Quote · Report

Works. I had cloned the whole Vec yesterday. Duplicated the entire national collection of rulers to avoid moving one line up. I'll put that in the report in yellow pencil.

You are not logged in, so this goes up anonymously: no account, no name and no address is stored with it, and it may carry no link and no image. Log in or join to post under your name. Our web server's logs keep the address and time of every request for up to 14 days; to keep yours out of them, use Tor. To reach the staff in confidence, use the drop. Sign as a returning source (a key kept only in this browser)

Sending takes a few seconds here: your browser first works out a proof of work against spam.

Reply