使用mem :: size_of ::< T>时,在编译时无法得知类型为'T'的值的大小.作为数组长度 [英] The size for values of type `T` cannot be known at compilation time when using mem::size_of::<T> as an array length
本文介绍了使用mem :: size_of ::< T>时,在编译时无法得知类型为'T'的值的大小.作为数组长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下功能:
fn make_array<T>()
where
T: Sized,
{
let bytes = [0u8; std::mem::size_of::<T>()];
}
无论出于何种原因,它都无法编译
For whatever reason it fails to compile
error[E0277]: the size for values of type `T` cannot be known at compilation time
--> src/lib.rs:5:23
|
5 | let bytes = [0u8; std::mem::size_of::<T>()];
| ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= help: consider adding a `where T: std::marker::Sized` bound
= note: required by `std::mem::size_of`
尽管事实是通用参数T
绑定了Sized
特征.对我来说没有任何意义.
This is despite the fact that there is a Sized
trait bound for the generic parameter T
. It doesn't make any sense to me.
为什么会发生这种情况,我该如何解决?
Why is this happening and how do I work around it?
推荐答案
似乎是错误#43408:数组长度不支持通用参数.
It seems to be bug #43408: Array lengths don't support generic parameters.
这篇关于使用mem :: size_of ::< T>时,在编译时无法得知类型为'T'的值的大小.作为数组长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文