gpt4 book ai didi

rust - 类型上的 `&mut` 和函数签名中的标识有什么区别?

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

<分区>

在一个库中,我正在编写以了解 Rust,我创建了一个特征 Decodeable。

pub trait Decodeable {
fn read_and_decode(&mut types::ReadSeeker) -> Result<Self, ::error::Error>;
}

然后我实现了类型:

impl Decodeable for u32 {
fn read_and_decode(&mut stream: types::ReadSeeker) -> Result<u32, error::Error> {
try!(stream.big_edian_read_u32());
}
}

这是失败的错误:

error: method `read_and_decode` has an incompatible type for trait:
expected &-ptr,
found trait types::ReadSeeker

我最终发现,如果我将函数签名更改为 read_and_decode(stream: &mut types::ReadSeeker),它会奏效。

我想了解 &mut stream: types::ReadSeekerstream: &mut types::ReadSeeker 之间的区别。这感觉像是使用rust 的一个基本部分,但除了它们实际上不同这一事实之外,我不知道有什么区别。

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