我想知道是否可以在从
if let Ok()
板条箱的 Result<Tuple(Struct, usize), DecodeError>
函数返回的 bincode::decode_from_slice
类型上使用 bincode
语法 https://docs.rs/bincode/2.0.0-rc.3/ bincode/fn.decode_from_slice.html 需要显式类型注释
我尝试了以下语法
struct A {
a: i32,
b: i32,
}
let mut recv_buff = [0u8; 1024];
if let Ok((a_obj, _): (A, usize)) = bincode::decode_from_slice(recv_buff, bincode::config::standard().with_big_endian()) {
}
错误
if let Ok((a_obj, _): (A, usize)) = bincode::decode_from_slice(recv_buff, bincode::config::standard()....
| ^ expected one of `)`, `,`, or `|`
您无法向
if let
添加注释,但您可以只指定所需的通用类型。
struct A {
a: i32,
b: i32,
}
let mut recv_buff = [0u8; 1024];
// We don't care about the config type so we use `_` as a placeholder for don't care here
if let Ok((a_obj, _) = bincode::decode_from_slice::<A, _>(
recv_buff, bincode::config::standard().with_big_endian()) {
}