Why is a temporary created here?

⚓ Rust    📅 2025-06-30    👤 surdeus    👁️ 5      

surdeus

Warning

This post was published 37 days ago. The information described in this article may have changed.

Hey folks,

I was playing around recently with the new let chains feature and read several old problems about let chains on GitHub. From this issue I found this code. Which generates the following compilation error:

   |
11 |     if let n = ()
   |            - binding `n` declared here
12 |         && let _ = B(&n)
   |                    --^^-
   |                    | |
   |                    | borrowed value does not live long enough
   |                    a temporary with access to the borrow is created here ...
13 |     {};
   |      -- ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `B`
   |      |
   |      `n` dropped here while still borrowed

Meanwhile I think I understand the drop order stuff about let chains and also the error in 2021 edition but what I currently don't understand is why the RHS of let _ = B(&n) results in a temporary in the first place according to the compiler error? I would understand when it creates a temporary for let _ = &B(&n) but not for let _ = B(&n).

Regards
keks

2 posts - 2 participants

Read full topic

🏷️ rust_feed