gpt4 book ai didi

function - 如何使用关联常量来定义数组的长度?

转载 作者:行者123 更新时间:2023-11-29 08:06:01 25 4
gpt4 key购买 nike

<分区>

我有一个特征,它表示可以通过 UDP 套接字发送的实体:

pub trait ToNetEnt {
const NET_SIZE: usize;

fn from_net(data: &[u8]) -> Self;
fn to_net(&self) -> &[u8];
}

虽然有一个关联的常量 NET_SIZE,但我不能在方法中使用它:

pub fn req<T: ToNetEnt>(&self) -> T {
let buf: [u8; T::NET_SIZE];
}

因为它导致了这个错误:

error[E0599]: no associated item named `NET_SIZE` found for type `T` in the current scope
--> src/main.rs:10:23
|
10 | let buf: [u8; T::NET_SIZE];
| ^^^^^^^^^^^ associated item not found in `T`
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `NET_SIZE`, perhaps you need to implement it:
candidate #1: `ToNetEnt`

我可以在此上下文中使用关联常量吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com