Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 无法将`*`作为不可变项借用,因为`*self`也作为可变项借用[E0502]_Rust - Fatal编程技术网

Rust 无法将`*`作为不可变项借用,因为`*self`也作为可变项借用[E0502]

Rust 无法将`*`作为不可变项借用,因为`*self`也作为可变项借用[E0502],rust,Rust,以下是我的全部代码: use std::collections::HashMap; pub struct Book<'a> { page: Vec<&'a str>, histories: HashMap<&'a str, &'a str>, } impl<'a> Book<'a> { pub fn new(page: Vec<&'a str>) -> Boo

以下是我的全部代码:

use std::collections::HashMap;

pub struct Book<'a> {
    page: Vec<&'a str>,
    histories: HashMap<&'a str, &'a str>,
}

impl<'a> Book<'a> {
    pub fn new(page: Vec<&'a str>) -> Book<'a> {
        let histories = HashMap::new();
        Book {
            page: page,
            histories: histories
        }
    }

    pub fn process(&mut self, line: &str, book_id: &'a str) {

        let page_c = self.get_page(book_id);
        println!("page_c {}", page_c);

        for history in self.histories.iter() {
            println!("histories...");
        }
    }

    fn get_page(&mut self, book_id: &'a str) -> &str {
        if !self.histories.contains_key(book_id) {
            println!("not history found for book {}", book_id);
            self.histories.insert(book_id, "history A");
        }
        self.histories.get(book_id).unwrap()
    }
}

fn main() {
    println!("Hello, world!");
    let mut pages = Vec::new();
    let st = "page1";
    pages.push(st);

    let mut biblio = Book::new(pages);

    let sentence = "this is a line of page";
    let book_id = "onebook";
    biblio.process(sentence, book_id);
}

我理解错误信息,但经过研究和阅读后,我不明白如何修复代码。

修复代码的最简单方法是不为第c页引入额外变量,而是直接使用
获取第
页的结果:

pub fn process(&mut self, line: &str, book_id: &'a str) {
    println!("page_c {}", self.get_page(book_id));

    for history in self.histories.iter() {
        println!("histories...");
    }
}
这样,当您进入
for
循环时,
self
就不会被借用,因为它只在调用
println
时被借用。如果您确实想为page_c引入一个变量,那么可以在一个额外的作用域中引入它,这样借用将在作用域的末尾(因此在循环之前):


堆栈溢出不是代码写入(或修复)服务。既然您表示理解错误信息并发现类似问题,那么您能否就如何帮助您理解问题而不只是为您编写解决方案向我们提供指导?前面的问题和答案有什么不足?同样的问题也有很多。基本上,侧边栏中所有“相关”的问答都应该检查出来。谢谢你的回答。我需要在循环后使用
page\u c
,类似这样:
}//循环结束println!(“第{}页,第{c);}
pub fn process(&mut self, line: &str, book_id: &'a str) {
    println!("page_c {}", self.get_page(book_id));

    for history in self.histories.iter() {
        println!("histories...");
    }
}
pub fn process(&mut self, line: &str, book_id: &'a str) {
    {
        let page_c = self.get_page(book_id);
        println!("page_c {}", page_c);
    } // page_c ceases to exist here, so the borrow of self ends
    for history in self.histories.iter() {
        println!("histories...");
    }
}