我定义了一个特征ReadTag
,它包含一个返回Self
的函数,但这样会产生一个错误:
trait ReadTag {
fn read_out(buf: &mut &[u8]) -> Option<Self>;
}
error[E0277]: the size for values of type `Self` cannot be known at compilation time
--> src/lib.rs:2:37
|
2 | fn read_out(buf: &mut &[u8]) -> Option<Self>;
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
通过添加Sized
作为超级特征修复了这个错误,这是有意义的,但为什么Sized
不像函数一样是默认的呢?
fn my_sized<T>(t: T) { } // all good, Sized is opt-out :)
fn my_unsized<T: ?Sized>(t: T) { } // not allowed