Preface: it's been a long while since I've written a Rust program of any kind, and I haven't really seriously used Rust beyond some short tutorial things. I can usually read it, though, and have studied parts of its semantics several times.
I watched RustConf 2021 - Compile-Time Social Coordination by Zac Burns - YouTube recently, and wanted to experiment with the ideas in that talk. They seemed like a really interesting way to design an API; you can use the Token idea in other languages, but you won't have the compiler to enforce it!
So I ended up creating this short program (corrected link) that I thought would demonstrate the compiler error, forcing the use of
drop like in the video. To my surprise, there are no compiler errors! Inserting the
drop line doesn't cause or remove any, which leads me to believe it's being done automatically… but as expected, inserting a use of connection after the second mutable borrow is a compiler error.
My question is twofold:
- Does the compiler automatically
drop(conn)in this example? (Is there a way to know that, say, by expanding the program with all the compiler-inserted pieces?)
- Is there a way to get this sample program to trigger the compiler error discussed in the video, so that I have to manually insert
drop(conn)? (This wouldn't necessarily be ideal for day-to-day use, but I would like to see it actually happen once .)