I am trying to understand how the compiler works with boxed closure objects.
Basically, this works fine:
let firstClosureBox = Box::new(|| ());
Explicitly typing the variable works as well:
let secondClosureBox: Box<Fn()> = Box::new(|| ());
However, this doesn’t work:
let thirdClosureBox = Box::<Fn()>::new(|| ());
/tmp/test_type.rs:8:27: 8:43 error: the trait `core::marker::Sized` is not implemented for the type `core::ops::Fn()` [E0277] /tmp/test_type.rs:8 let fifthClosureBox = Box::<Fn()>::new(|| ()); ^~~~~~~~~~~~~~~~
If I understand correctly, this is because
Box::new() is defined within
impl<T>, not within
impl<T: ?Sized>, and because
Fn is not
But then, how and why does the rust compiler accept and encode
secondClosureBox without issue?
Basically, I expected
thirdClosureBox to be just as valid, but they are not, and I’m not exactly sure as to why that is.