O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exercises

  1. Remove mut from the closure declaration line. Why does that make compilation fail?

Solution: Because the closure becomes mutable when values it captures are mutated inside it, even when (like in our example) all of those values have copy semantics.

  1. Remove move from the closure declaration line. What's the effect and why?

Solution: The closure turns into having the default borrow semantics, which means that it receives a borrowing of the outer_scope_x variable, instead of a copy of it.

  1. It looks like we wouldn't need the block starting from line 4 and ending on line 11. Try to remove that and see if that's true.

Solution: We actually don't! The last println! wants access to outer_scope_x, which is being borrowed mutably by the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required