Saltar al contenido ForoHackers

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

CGestión de memoriaRust
#7366 · · Citar · Denunciar

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 · · Citar · Denunciar

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 · · Citar · Denunciar

@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 · · Citar · Denunciar

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 · · Citar · Denunciar

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.

No has iniciado sesión, así que esto se publica como anónimo: no se guarda con ello ninguna cuenta, nombre ni dirección, y no puede llevar enlaces ni imágenes. Inicia sesión o únete para publicar con tu nombre. Los registros de nuestro servidor web guardan la dirección y la hora de cada petición hasta 14 días; para que la tuya no quede en ellos, usa Tor. Para escribir al equipo de forma confidencial, usa el buzón. Firmar como fuente habitual (una clave guardada solo en este navegador)

Enviar tarda unos segundos aquí: tu navegador resuelve antes una prueba de trabajo contra el spam.

Responder