Misc 9 @adotinthevoid @jdonszelmann @Victoronz @GoldsteinE @WaffleLapkin

Warning: this quiz is still "work-in-progress", some questions might not have good explanations (or any at all), formatting/structure/titles/etc are not final, and so on. You might want to return here on a later date.

struct Unit;
struct Tuple();
struct Struct {}

fn main() {
    Unit;
    Tuple;
    Struct;

    Unit();
    Tuple();
    Struct();

    Unit {};
    Tuple {};
    Struct {};

    Unit = Unit;
    Tuple = Tuple();
    Struct = Struct {};

    Unit { .. } = Unit { ..Unit };
    Tuple { .. } = Tuple { ..Tuple() };
    Struct { .. } = Struct { ..Struct {} };
}
Solution
error[E0423]: expected value, found struct `Struct`
 --> examples/misc_9.rs:8:5
  |
3 | struct Struct {}
  | ---------------- `Struct` defined here
...
8 |     Struct;
  |     ^^^^^^ help: use struct literal syntax instead: `Struct {}`

error[E0423]: expected value, found struct `Struct`
  --> examples/misc_9.rs:20:5
   |
3  | struct Struct {}
   | ---------------- `Struct` defined here
...
20 |     Struct = Struct {};
   |     ^^^^^^ help: use struct literal syntax instead: `Struct {}`

error[E0618]: expected function, found struct `Unit`
  --> examples/misc_9.rs:10:5
   |
1  | struct Unit;
   | ----------- struct `Unit` defined here
...
10 |     Unit();
   |     ^^^^--
   |     |
   |     call expression requires function
   |
help: `Unit` is a unit struct, and does not take parentheses to be constructed
   |
10 -     Unit();
10 +     Unit;
   |

error[E0423]: expected function, tuple struct or tuple variant, found struct `Struct`
  --> examples/misc_9.rs:12:5
   |
3  | struct Struct {}
   | ---------------- `Struct` defined here
...
12 |     Struct();
   |     ^^^^^^^^ help: use struct literal syntax instead: `Struct {}`

error[E0070]: invalid left-hand side of assignment
  --> examples/misc_9.rs:19:11
   |
19 |     Tuple = Tuple();
   |     ----- ^
   |     |
   |     cannot assign to this expression

Some errors have detailed explanations: E0070, E0423, E0618.
For more information about an error, try `rustc --explain E0070`.
error: could not compile `code` (example "misc_9") due to 5 previous errors

Unit; on it's own is fine, because Unit is declared as a unit struct, so Unit is a constant of type Unit.

Tuple; on it's own is fine, because Tuple as a value is the constructor for Tuple, with type fn() -> Tuple.

Struct; is a compiler error, because Struct only exists as a type, and never a value.

Only Tuple structs can be initialized with parentheses, because it's actually calling a constructor function.

No matter what kind a struct is, it can always be initialized with braces (even if not declared with them). Therefore all 3 statements are OK.

You cannot generally use a struct name as the left hand side of an assignment. However, with the Unit = Unit works because this is a destructuring assignment where Unit on the left is a constant pattern.

Destructuring assignment on a unit structs works as any other struct, and struct update syntax as well, even when there are no fields. Therefore, the last three statements work.