哈希映射宏拒绝类型检查,失败并出现误导性(并且看似有缺陷)的错误消息?

2024-03-04

我这里有这两个相关的宏:

#[macro_export]
macro_rules! hash_map {
    ( $( $key:expr => $value:expr ),* ) => {
        {
            use ::std::iter::FromIterator;
            ::std::collections::HashMap::from_iter(&[$(($key, $value)),*])
        }
    };
}

#[macro_export]
macro_rules! hash_set {
    ( $( $x:expr ),* ) => {
        {
            use ::std::iter::FromIterator;
            ::std::collections::HashSet::from_iter(&[$($x),*])
        }
    };
}

我主要在测试中使用它,如下所示:

assert!(foo.one == hash_map!{});
assert!(foo.two == hash_set![String::from("some value")]);

但由于某种原因,我从中得到了一条非常具有误导性和荒谬的错误消息:

error[E0271]: type mismatch resolving `<&[_; 0] as std::iter::IntoIterator>::Item == (_, _)`
   --> src/common.rs:M:N
    |
6   |             ::std::collections::HashMap::from_iter(&[$(($key, $value)),*])
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected reference, found tuple
    | 
   ::: src/foo.rs:M:N
    |
154 |         assert!(foo.one == hash_map!{});
    |                                         ----------- in this macro invocation
    |
    = note: expected type `&_`
               found type `(_, _)`
    = note: required by `std::iter::FromIterator::from_iter`

error[E0308]: mismatched types
   --> src/common.rs:M:N
    |
16  |             ::std::collections::HashSet::from_iter(&[$($x),*])
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found reference
    | 
   ::: src/foo.rs:M:N
    |
150 |     fn it_can_do_things() {
    |                                - help: try adding a return type: `-> std::collections::HashSet<&std::string::String, _>`...
155 |         assert!(foo.two == hash_set![String::from("some value")]);
    |                                  -------------------------------------------- in this macro invocation
    |
    = note: expected type `std::collections::HashSet<std::string::String, std::collections::hash_map::RandomState>`
               found type `std::collections::HashSet<std::string::String, std::collections::hash_map::RandomState>`
               found type `std::collections::HashSet<&std::string::String, _>`

我首先尝试将元组本身作为引用,但这没有帮助。

error[E0271]: type mismatch resolving `<&[_; 0] as std::iter::IntoIterator>::Item == (_, _)`
   --> src/common.rs:M:N
    |
6   |             ::std::collections::HashMap::from_iter(&[$(&($key, $value)),*])
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected reference, found tuple
    | 
   ::: src/foo.rs:M:N
    |
154 |         assert!(foo.one == hash_map!{});
    |                                         ----------- in this macro invocation
    |
    = note: expected type `&_`
               found type `(_, _)`
    = note: required by `std::iter::FromIterator::from_iter`

我的目标是尝试让哈希映射保留并在单个批次中添加所有内容,但我就是无法让它正常工作。请注意,我正在创建的哈希映射/集拥有其值。


最小复制:

fn main() {
    use std::collections::HashMap;
    use std::iter::FromIterator;

    let _ = HashMap::from_iter([("key", "value")].iter());
}

您必须迭代拥有的元组,而不是借用的元组,因此会出现奇怪的错误消息:

fn main() {
    use std::collections::HashMap;
    use std::iter::FromIterator;

    let _: HashMap<_, _> = HashMap::from_iter([("key", "value")].iter().cloned());
}

您还可以使用collect正如文档中所述FromIterator:

FromIterator's from_iter很少显式调用,而是通过以下方式使用Iterator's collect方法。看collect的文档以获取更多示例。

fn main() {
    use std::collections::HashMap;

    let _: HashMap<_, _> = [("key", "value")].iter().cloned().collect();
    // or
    let _: HashMap<_, _> = vec![("key", "value")].into_iter().collect();
}

请注意,错误消息更容易理解collect:

error[E0277]: the trait bound `std::collections::HashMap<_, _>: std::iter::FromIterator<&(&str, &str)>` is not satisfied
 --> src/main.rs:4:54
  |
4 |     let _: HashMap<_, _> = [("key", "value")].iter().collect();
  |                                                      ^^^^^^^ a collection of type `std::collections::HashMap<_, _>` cannot be built from an iterator over elements of type `&(&str, &str)`
  |
  = help: the trait `std::iter::FromIterator<&(&str, &str)>` is not implemented for `std::collections::HashMap<_, _>`

最后一行更有帮助:迭代器必须迭代元组而不是对元组的引用:

impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S> 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

哈希映射宏拒绝类型检查,失败并出现误导性(并且看似有缺陷)的错误消息? 的相关文章

随机推荐