diff --git a/questions/006-value-of-assignment.md b/questions/006-value-of-assignment.md new file mode 100644 index 0000000..2f53520 --- /dev/null +++ b/questions/006-value-of-assignment.md @@ -0,0 +1,39 @@ +Answer: 0 +Difficulty: 1 + +# Hint + +There are two variables named `a`. What is the type of each one? + +# Explanation + +There are two variables named `a`, one shadowing the other. The program is +equivalent to: + +```rust +let a; +let b = a = true; +print!("{}", mem::size_of_val(&b)); +``` + +Further, the value being assigned to `b` is the expression `a = true`. + +In Rust, assignment expressions always have the value `()`. Simplified some +more, the quiz code is equivalent to: + +```rust +let a = true; +let b = (); +print!("{}", mem::size_of_val(&b)); +``` + +Refer to the documentation of [`size_of_val`] for a specification of its +behavior, but in this case it is being instantiated with `T = ()` and we end up +printing the value of `size_of::<()>()`. + +[`size_of_val`]: https://doc.rust-lang.org/std/mem/fn.size_of_val.html + +`()` is one example of a [*zero-sized type*][zst] or ZST and is represented by +zero bytes of data at runtime, so the program prints `0`. + +[zst]: https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts diff --git a/questions/006-value-of-assignment.rs b/questions/006-value-of-assignment.rs new file mode 100644 index 0000000..029e410 --- /dev/null +++ b/questions/006-value-of-assignment.rs @@ -0,0 +1,7 @@ +use std::mem; + +fn main() { + let a; + let a = a = true; + print!("{}", mem::size_of_val(&a)); +}