Enum variants do not have their own type. There is only a type of enumeration itself. Check out this example:
enum Thing<T> {
One,
Two(T),
}
fn main() {
let a = Thing::One;
let b = Thing::Two(true);
}
Type bis this Thing<bool>. Type does not mention Two. The same thing should happen for a, but there is nothing that the compiler could use to output the value T, so you must explicitly specify it:
let a = Thing::One::<u8>;
- , - Option:
fn main() {
// let a = None;
let a = None::<u8>;
let b = Some(true);
}
T, .
. max ( , ):
enum Foo {
One(u64),
Two(u8),
}
fn main() {
println!("{}", std::mem::size_of::<u64>());
// 8
println!("{}", std::mem::size_of::<u8>());
// 1
println!("{}", std::mem::size_of::<Foo>());
// 16
}
, :
fn main() {
let a = Some(true);
let b = None::<bool>;
println!("{}", std::mem::size_of_val(&a));
// 2
println!("{}", std::mem::size_of_val(&b));
// 2
}
, None :
fn main() {
let a = None::<u8>;
println!("{}", std::mem::size_of_val(&a));
// 2
let b = None::<u64>;
println!("{}", std::mem::size_of_val(&b));
// 16
}
, , None. .