简体   繁体   English

Rust 编译错误:'发生移动是因为 `self.styles` 的类型为 `HashMap<&str, &str>`'

[英]Rust compilation error: 'move occurs because `self.styles` has type `HashMap<&str, &str>`'

I have been trying to write simple logger "class" that can display test message in colors.我一直在尝试编写简单的记录器“类”,它可以在 colors 中显示测试消息。 I am running into the compilation error.我遇到了编译错误。 What am i doing wrong and how can i fix it?我做错了什么,我该如何解决? (please note i'd like to fix this particular issue not to replace what i am doing with some coloring crate, etc... it's a learning exercise for me). (请注意,我想解决这个特殊问题,而不是用一些着色箱等代替我正在做的事情......这对我来说是一个学习练习)。

error[E0507]: cannot move out of `self.styles` which is behind a shared reference
  --> src/main.rs:21:30
   |
21 |         for (style, term) in self.styles {
   |                              ^^^^^^^^^^^ move occurs because `self.styles` has type `HashMap<&str, &str>`, which does not implement the `Copy` trait

Code...代码...

use std::collections::HashMap;

struct Logger <'a> {
    styles: HashMap<&'a str, &'a str>,
}

impl Logger <'_> {
    fn new() -> Logger <'static> {
        let mut logger = Logger{styles: HashMap::new()};
        logger.styles.insert("</>", "\x1b[0m");
        logger.styles.insert("<WARN>", "\x1b[1m\x1b[93m");
        logger.styles.insert("<CRIT>", "\x1b[41m");
        logger.styles.insert("<INFO>", "\x1b[1m");
        logger
    }

    fn log(&self, message: &str) {
        let mut output = String::new();
        for (style, term) in self.styles {
            output = message.replace(style, term);
        }
        println!("{}", output)
    }
}

fn main() {
    let logger = Logger::new();

    logger.log("<INFO>Logger</> written in <CRIT>Rust</> language, <WARN>2021</>");
}

The for loop has an implicit call to .into_iter() . for 循环隐式调用.into_iter() From the docs (emphasis mine):从文档(强调我的):

Creates a consuming iterator, that is, one that moves each key-value pair out of the map in arbitrary order.创建一个消费迭代器,即以任意顺序将每个键值对移出 map 的迭代器。 The map cannot be used after calling this. map 调用后无法使用。

So the for loop is trying to move self.styles since it doesn't implement the Copy trait.所以 for 循环试图移动self.styles因为它没有实现Copy特征。 Try borrowing instead:尝试借用:

for (&style, &term) in &self.styles {
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;移动发生是因为值具有类型&#39; Rust 错误 - 'move occurs because value has type' Rust error Rust:发生移动是因为类型为 `ReadDir`,它没有实现 `Copy` 特征 - Rust: move occurs because has type `ReadDir`, which does not implement the `Copy` trait 带有 str 键的 HashMap 的 Rust lang 生命周期不匹配 - Rust lang lifetime mismatch for HashMap with str key 发生 Rust 文件树移动是因为 `subtree` 的类型为 `trees::Tree<PathBuf> `,它没有实现 `Copy` 特征 - Rust File Tree move occurs because `subtree` has type `trees::Tree<PathBuf>`, which does not implement the `Copy` trait Rust:“不能移出`self`,因为它是借来的”错误 - Rust: “cannot move out of `self` because it is borrowed” error 在 Rust 中,为什么 '[T]' 和 'str' 切片类型不是语法错误? - In Rust, why is '[T]' and 'str' slice type not a syntax error? 在 Rust 中使用 generics 时,“在编译时无法知道 `str` 类型的值的大小” - When using generics in Rust, “the size for values of type `str` cannot be known at compilation time” Rust“性状`借<char> ` 没有为 `&amp;str` 实现“错误</char> - Rust "the trait `Borrow<char>` is not implemented for `&str`" Error 无法移出位于共享引用移动后面的 `*handle`,因为 `*handle` 具有类型 - cannot move out of `*handle` which is behind a shared reference move occurs because `*handle` has type Rust 代码有问题并说:没有字段 `query` on type `std::result::Result<config, &str> `</config,> - Rust code has a problem and say :no field `query` on type `std::result::Result<Config, &str>`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM