R: AsRef<[T]>不应该适用于T=u64吗?



关于以下内容:

pub fn a<
T: Copy,
R: AsRef<[T]> 
>(
a_fn: &dyn Fn(&[R], &mut [u64]),
) 
{
let mut result = vec![0, 3];
b(&[1,2,3], result.as_mut_slice());
}


fn b<T: Copy, R: AsRef<[T]>>(_: &[R], _: &mut [u64]) {
unimplemented!();
}

fn c() {
a::<u64, &[u64]>(&b);
}

https://play.rust-lang.org/?version=stable&mode=调试&edition=2021&gist=43d054772b15fa575cc75e87b4eb34d9

我试图对类型R进行泛型处理,但最终使用了u64的切片。然而,看起来[integer]并没有实现AsRef<[T]>

错误:

Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `{integer}: AsRef<[_]>` is not satisfied
--> src/lib.rs:9:5
|
9  |     b(&[1,2,3], result.as_mut_slice());
|     ^ the trait `AsRef<[_]>` is not implemented for `{integer}`
|
= help: the following implementations were found:
<&T as AsRef<U>>
<&mut T as AsRef<U>>
<Arc<T> as AsRef<T>>
<Box<T, A> as AsRef<T>>
and 38 others
note: required by a bound in `b`
--> src/lib.rs:13:18
|
13 | fn b<T: Copy, R: AsRef<[T]>>(_: &[R], _: &mut [u64]) {
|                  ^^^^^^^^^^ required by this bound in `b`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error

b更改为R&R,而不是&[R]&[R] == &[AsRef<[T]>]有嵌套的切片,我认为这不是你的意图。

fn main() {
let mut result = vec![0, 3];
b(&[1,2,3], result.as_mut_slice());
}

fn b<T: Copy, R: AsRef<[T]>>(_: R, _: &mut [u64]) {
unimplemented!();
}

游乐场

相关内容

  • 没有找到相关文章

最新更新