100-exercises-to-learn-rust icon indicating copy to clipboard operation
100-exercises-to-learn-rust copied to clipboard

Test cases for 07_threads/06_interior_mutability should be explained

Open iurii-kyrylenko opened this issue 2 months ago • 0 comments

The following test case cannot be understood without explanation of how rust handles the temporary values: https://github.com/mainmatter/100-exercises-to-learn-rust/blob/d347d1f72ee77b96b2d51f0405292fd0590e8162/exercises/07_threads/06_interior_mutability/src/lib.rs#L28-L33 The use of the wildcard _ as the binding target in the test case it_works() signals to the compiler that the value is only temporary, leading to its immediate drop at the statement's conclusion. In contrast, assigning to a named variable in the test case multiple() ensures the variable's full lifetime within its defining scope: https://github.com/mainmatter/100-exercises-to-learn-rust/blob/d347d1f72ee77b96b2d51f0405292fd0590e8162/exercises/07_threads/06_interior_mutability/src/lib.rs#L35-L45 To suppress the compiler warning, unused vars a and b should be prefixed by _.

iurii-kyrylenko avatar Oct 11 '25 09:10 iurii-kyrylenko