How does comparison of a slice with an array work in Rust?

牧云@^-^@ 提交于 2019-12-13 17:41:02

问题


Why would [1,1] == &[1,1] not even compile (presumably because they're different types), yet the following snippet compiles and runs fine.

let a: [i32; 100] = [1; 100];
let b: &[i32] = &a[1..3];
if b == [1, 1] { // comparison with &[1, 1] works as well
    println!("it works"); // this does get printed
}

回答1:


Right now, arrays in Rust are somewhat special because Rust lacks type-level integers. You can't write a function fn f<T, N>(array: &[T; N]). Likewise, you can't implement a trait which is generic over N.

The standard library provides some trait implementations for array lengths ranging from 0 to 32 to mitigate this issue, which is why b == [1,1] works. There's an implementation of the trait PartialEq for this case:

impl<'a, 'b, A, B> PartialEq<[A; 2]> for &'b [B] 
where B: PartialEq<A>

However, the trait PartialEq<&[A; 2]> is not implemented for [B; 2]. Thus you cannot compare [1, 1] and &[1, 1]. b == [1; 33] will not work in your example too, as there's no implementations for arrays longer than 32 elements.

But there's ongoing effort to bring type-level integers into Rust. RFC 2000 is the latest proposal.

For the time being you can rely on implicit conversion from reference to the array to reference to slice. Like this

fn f<T>(slice: &[T]) {}

f(&[1, 2, 3, 4]);


来源:https://stackoverflow.com/questions/44351328/how-does-comparison-of-a-slice-with-an-array-work-in-rust

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!