无法在异步函数中更改 pathBuff/path 变量

问题描述

我不确定是否应该在此处或在代码审查中发布此内容代码审查似乎只有正常运行的代码

所以我有很多我不明白的问题。 (我是菜鸟)可以在这里找到完整的代码https://github.com/NicTanghe/winder/blob/main/src/main.rs 主要问题在这里

let temp = location_loc1.parent().unwrap();
location_loc1.push(&temp);

我尝试了各种方法解决将借用作为可变或参考借用的问题, 我似乎无法让它工作。

我尝试的每件事都出现了一组不同的错误。 此外,如果这是重复的,我很抱歉,但是寻找错误的单独解决方案只是给了我一个不同的错误。围成一圈。

功能齐全

async fn print_events(mut selector_loc1:i8,location_loc1: PathBuf) {
    let mut reader = EventStream::new();

    loop {
        //let delay = Delay::new(Duration::from_millis(1_000)).fuse();
        let mut event = reader.next().fuse();

        select! {
            // _ = delay => {
            //      print!("{esc}[2J{esc}[1;1H{}",esc = 27 as char,); 
                 
            // },maybe_event = event => {
                match maybe_event {
                    Some(Ok(event)) => {
                        //println!("Event::{:?}\r",event);
                        
                        // if event == Event::Mouse(MouseEvent::Up("Left").into()) {
                        //     println!("Cursor position: {:?}\r",position());
                        // }   
                        print!("{esc}[2J{esc}[1;1H{}",); 
                        if event == Event::Key(KeyCode::Char('k').into()) {
                            if selector_loc1 > 0 {
                                selector_loc1 -= 1;
                            };
                            //println!("go down");
                            //println!("{}",selected)

                        }   else if event == Event::Key(KeyCode::Char('j').into()) {
                            selector_loc1 += 1;
                            //println!("go up");
                            //println!("{}",selected)
                        }   else if event == Event::Key(KeyCode::Char('h').into()) {


                        //-----------------------------------------
                        //-------------BackLogic-------------------
                        //-----------------------------------------
                            let temp = location_loc1.parent().unwrap();
                            location_loc1.push(&temp);
                            
                        //------------------------------------------
                        //------------------------------------------

                        }   else if event == Event::Key(KeyCode::Char('l').into()) {
                            //go to next dir

                        }   if event == Event::Key(KeyCode::Esc.into()) {
                            break;
                        }

                        printtype(location_loc1,selector_loc1);

                    }
                    Some(Err(e)) => println!("Error: {:?}\r",e),None => break,}
            }
        };
    }
}

还有,好像用

use async_std::path::{Path,PathBuf};

使 rust 无法识别 unwrap() 函数 → 我将如何使用 using ?

解决方法

您的代码有两个问题。

  1. 您的 PathBuf 是不可变的。无法修改不可变对象,除非它们支持 interior mutabilityPathBuf 没有。因此,您必须使变量可变。您可以像这样在它前面添加 mut
async fn print_events(mut selector_loc1:i8,mut location_loc1: PathBuf) {

或者你可以重新绑定:

let mut location_loc1 = location_loc1;
  1. 你不能同时借用可变和不可变 - the mutable borrows are exclusive!鉴于方法 .parent() 借用了缓冲区,您必须创建一个临时拥有的值:
// the PathBuf instance
let mut path = PathBuf::from("root/parent/child");

// notice the .map(|p| p.to_owned()) method - it helps us avoid the immutable borrow
let parent = path.parent().map(|p| p.to_owned()).unwrap();

// now it's fine to modify it,as it's not borrowed
path.push(parent);

你的第二个问题:

此外,似乎使用 use async_std::path::{Path,PathBuf}; 使 rust 无法识别 unwrap() 函数 → 我将如何使用 using ?

async-std 版本只是 std 的 PathBuf 的包装器。它只是委托给标准实现,所以它的行为不应该不同

// copied from async-std's PathBuf implementation
pub struct PathBuf {
    inner: std::path::PathBuf,}