如何在 Rust 中注释空切片的类型?

How do I annotate the type of an empty slice in Rust?

假设我想在测试中将 Vec<String> 与文字空列表进行比较。

(我知道在实践中我可以检查 is_empty(),但我想了解 Rust 类型在这里是如何工作的,我认为断言相等会在失败时给出更清晰的信息。)

如果我说

    let a: Vec<String> = Vec::new();
    assert_eq!(a, []);

get an error那个

error[E0282]: type annotations needed
 --> src/main.rs:3:5
  |
3 |     assert_eq!(a, []);
  |     ^^^^^^^^^^^^^^^^^^ cannot infer type
  |
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

我认为问题在于 rustc 无法判断我指的是 String 的空列表,还是 &str 的空列表,或者其他什么东西?

如何将所需的类型注释添加到 [] 文字上?

这取决于 not-yet-stable type ascription feature,还是有稳定的方法来指定它?

今天 works 的一种方法是 as 指定类型和长度的转换:

assert_eq!(a, [] as [&str; 0]);