error[E0423]: expected value, found struct `Toto`
--> src\lib.rs:11:10
|
4 | struct Toto {}
| -------------- `Toto` defined here
...
11 | &Toto
| ^^^^ help: use struct literal syntax instead: `Toto {}`
For more information about this error, try `rustc --explain E0423`.
error: could not compile `dyn-tray` (lib) due to previous error
Why can I use &Toto or &Toto {} when Toto is define as struct Toto; but only &Toto {} when Toto is defined as struct Toto {}?
I could be misremembering this, but I seem to recall this not always being true. Specifically, you couldn't use Toto {} in the case of struct Toto;. One of the arguments for changing this was specifically to allow macros to write literals and patterns for "structs with no fields" with the same syntax as "struct with named fields". Basically just treating them as a degenerate case.
That said, I was not aware this also worked for tuple structs. Thank you for using that example!
Strictly speaking, declaring a constant implies that:
const FOO: u32 = 10;
fn main() {
for i in [0, 10, 17, 20, 30] {
match i {
17 => println!("seventeen"),
FOO => println!("foo"),
num => println!("number is: {num}"),
}
}
}
number is: 0
foo
seventeen
number is: 20
number is: 30
Interesting, but this doesn't quite work for structs:
struct Toto {}
const Toto: Toto = Toto {};
fn main() {
match (Toto {}) {
Toto => {}
}
}
error: to use a constant of type `Toto` in a pattern, `Toto` must be annotated with `#[derive(PartialEq, Eq)]`
--> src/main.rs:7:9
|
7 | Toto => {}
| ^^^^
|
So is there a difference between struct Toto; and struct Toto {}, besides how you can instantiate them?
I thought maybe the former always refers to one singular object, but I made a playground where I implemented Drop for it and it got called twice when I instantiated it twice, so I'm clearly wrong about that.