Hello,
I recently came across this Rust quiz and I have several questions. Sorry for the question-bombing in advance ![]()
Question #2
I don't understand how h performs a bitwise-AND with an empty block. Shouldn't h result in a tuple with a reference to S(3)? (i.e. let h = (&S(3));).
Question #6
I do understand why this is possible, but I found it surprising that Rust doesn't warn about it. Maybe someone could give me an example of where such code might be useful? I can't think of any and it only seems like a footgun to me. This code compiles without warnings and I think any C/C++ developer would expect a and b to be both 3.
Question #13
I just didn't understand this one, I need an ELI5 about this. What is the memory address of a zero-sized type? I checked this on a playground and it does indeed look like there is an address for a zero-sized type.
Question #16
I think this is the first time that I've seen some Rust code and genuinely just thought to myself: "Wtf?". I have known that Rust does not support pre-/post-decrement operators since the first day I read the Rust book, and even knowing about it, it didn't even cross my mind that they weren't supported when I looked at the code in the quiz. I was also very surprised to see that this code compiles without warnings.
Question #19
I thought I understood the quiz code, but I got confused when I read the answer. If I run this code with let x = s instead (playground), it behaves the same. However, there is another example in question #28 that behaves as described in this question (#19).
Question #23
How come the S.f() syntax is valid? I would expect this to require S::f(). Is it because it's a zero-sized type?
Question #25
I got confused here because I learned in this quiz that in Rust there are two namespaces, a value namespace and a type namespace.
I would expect that when you write let S = f(); the compiler binding to a variable S rather than it doing pattern matching on a type S. On the other hand I would expect let _ : S = f(); to do the pattern matching.
What I also found interesting is that the same code but with let S: S = f(); also prints 212, but I'd expect that to print 12 instead, because the value returned by f() should now be indeed bound to the variable S, then 1 would be printed and then the variable S would be dropped. This, however, is not the case (playground).
Question #29
I'm confused about the 1-tuple concept and the trailing comma. Why is (0) not a valid tuple? Where can I read more about this?
Question #31
I don't understand why wwt.f() and wwwwwt.f() work.
Talking about wwt.f():
wwt.f(): The search order is
&&T->&&&T->&mut &&T->&T, and we're done. Upon invocation, the function prints 1.
Where does the last -> &T come from? Shouldn't the search order just be &&T -> &&&T -> &mut &&T?