我看到了解决方法,它们有点长。我是否缺少 Rust 的某个功能或简单的解决方案(重要:不是解决方法)。我觉得我应该能够用一个简单的宏来做到这一点,但 arrayref crate 实现不是我想要的。这是一个需要添加到 Rust 的功能吗?还是在较小的范围内从固定大小的数组创建固定大小的切片是一件坏事。基本上我想做的是这个;
fn f(arr:[u8;4]){
arr[0];
}
fn basic(){
let mut arr:[u8;12] = [0;12];
// can't I borrow the whole array but but a fixed slice to it?
f(&mut arr[8..12]); // But this is know on compile time?
f(&mut arr[8..12] as &[u8;4]); // Why can't I do these things?
}
Run Code Online (Sandbox Code Playgroud)
我想要的可以通过下面的代码来实现(来自其他线程)
use array_ref;
fn foo(){
let buf:[u8;12] = [0;12];
let (_, fixed_slice) = mut_array_refs![
&mut buf,
8,
4
];
write_u32_into(fixed_slice,0);
}
fn write_u32_into(fixed_slice:&mut …Run Code Online (Sandbox Code Playgroud)