这里的 mut 用法是什么?
What does the mut usage here?
看评论,这里的mut self_encoded
是什么意思?
pub trait DecodeLength {
fn len(self_encoded: &[u8]) -> Result<usize, Error>;
}
// This can compile.
impl DecodeLength for i32 {
// here
fn len(mut self_encoded: &[u8]) -> Result<usize, Error> {
usize::try_from(u32::from(Compact::<u32>::decode(&mut self_encoded)?))
.map_err(|_| "Failed convert decoded size into usize.".into())
}
}
// This way not works since the signature of this len is not correctly.
// impl DecodeLength for i32 {
// fn len(self_encoded: &mut [u8]) -> Result<usize, Error> {
// Ok(2)
// }
// }
你必须记住当你像这样改变你的fn
fn len(mut self_encoded: &[u8]) -> usize {
2
}
你没有改变实际输入,你的输入仍然是&[u8]
,你只是告诉编译器输入变量的值可以改变
像这样
impl DecodeLength for i32 {
// here
fn len(mut self_encoded: &[u8]) -> usize {
self_encoded = b"123";
2
}
}
但是当您将输入类型 &[u8]
更改为 &mut [u8]
时,您选择了新的输入类型
现在编译器给你一个错误并说“预期 &[u8]
但发现 &mut [u8]
// Error: expected fn pointer `fn(&[u8]) -> _` found fn pointer `fn(&mut [u8]) -> _
impl DecodeLength for i32 {
fn len(self_encoded: &mut [u8]) -> usize {
2
}
}
记住&[u8]
和&mut [u8]
是不同的,有不同的用途
[编辑]
当你的函数没有 return Result
时,你不能在函数中使用 ?
[编辑 2]
看下面代码
impl DecodeLength for i32 {
// here
fn len(mut self_encoded: &[u8]) -> usize {
let mut_ref = &mut self_encoded; // this give you `&mut &[u8]`
let mut_ref2 = self_encoded.as_mut(); // Error: `as_mut` is only valid for `&mut` references
1
}
}
您不能将 &[u8]
类型更改为 &mut [u8]
,这会给您一个错误
简单的方法是像这样更改您的 DecodeLength
pub trait DecodeLength {
fn len(self_encoded: &mut [u8]) -> Result<usize, Error>;
}
&[u8]
它是对一个字节切片的引用,这个切片的值不能改变并且是不可变的,你只能读取它
fn immutable_slice(input: &[u8]) {
//input[0] = b'a'; // Give you an Error
if input[0] == b'a' {
println!("Index 0 is 'a'");
}
}
&mut [u8]
它是对可编辑字节片的引用,它可以在 fn
内部更改
fn mutable_slice(input: &mut [u8]) {
input[0] = b'a';
println!("{:?}", input);
}
你可以这样测试
fn main() {
let numbers1: Vec<u8> = vec![1, 2, 3, 4, 5];
immutable_slice(&numbers1);
let mut numbers2: Vec<u8> = vec![1, 2, 3, 4, 5];
mutable_slice(&mut numbers2);
}
看评论,这里的mut self_encoded
是什么意思?
pub trait DecodeLength {
fn len(self_encoded: &[u8]) -> Result<usize, Error>;
}
// This can compile.
impl DecodeLength for i32 {
// here
fn len(mut self_encoded: &[u8]) -> Result<usize, Error> {
usize::try_from(u32::from(Compact::<u32>::decode(&mut self_encoded)?))
.map_err(|_| "Failed convert decoded size into usize.".into())
}
}
// This way not works since the signature of this len is not correctly.
// impl DecodeLength for i32 {
// fn len(self_encoded: &mut [u8]) -> Result<usize, Error> {
// Ok(2)
// }
// }
你必须记住当你像这样改变你的fn
fn len(mut self_encoded: &[u8]) -> usize {
2
}
你没有改变实际输入,你的输入仍然是&[u8]
,你只是告诉编译器输入变量的值可以改变
像这样
impl DecodeLength for i32 {
// here
fn len(mut self_encoded: &[u8]) -> usize {
self_encoded = b"123";
2
}
}
但是当您将输入类型 &[u8]
更改为 &mut [u8]
时,您选择了新的输入类型
现在编译器给你一个错误并说“预期 &[u8]
但发现 &mut [u8]
// Error: expected fn pointer `fn(&[u8]) -> _` found fn pointer `fn(&mut [u8]) -> _
impl DecodeLength for i32 {
fn len(self_encoded: &mut [u8]) -> usize {
2
}
}
记住&[u8]
和&mut [u8]
是不同的,有不同的用途
[编辑]
当你的函数没有 return Result
?
[编辑 2]
看下面代码
impl DecodeLength for i32 {
// here
fn len(mut self_encoded: &[u8]) -> usize {
let mut_ref = &mut self_encoded; // this give you `&mut &[u8]`
let mut_ref2 = self_encoded.as_mut(); // Error: `as_mut` is only valid for `&mut` references
1
}
}
您不能将 &[u8]
类型更改为 &mut [u8]
,这会给您一个错误
简单的方法是像这样更改您的 DecodeLength
pub trait DecodeLength {
fn len(self_encoded: &mut [u8]) -> Result<usize, Error>;
}
&[u8]
它是对一个字节切片的引用,这个切片的值不能改变并且是不可变的,你只能读取它
fn immutable_slice(input: &[u8]) {
//input[0] = b'a'; // Give you an Error
if input[0] == b'a' {
println!("Index 0 is 'a'");
}
}
&mut [u8]
它是对可编辑字节片的引用,它可以在 fn
内部更改fn mutable_slice(input: &mut [u8]) {
input[0] = b'a';
println!("{:?}", input);
}
你可以这样测试
fn main() {
let numbers1: Vec<u8> = vec![1, 2, 3, 4, 5];
immutable_slice(&numbers1);
let mut numbers2: Vec<u8> = vec![1, 2, 3, 4, 5];
mutable_slice(&mut numbers2);
}