使用命令从rust中获取文件源

使用命令从rust中获取文件源,rust,command-line-interface,Rust,Command Line Interface,我正在使用命令板条箱来获取文件。我正在使用相同的命令行执行ls-a,然后再进行源代码生成。当我列出目录内容时,将要源文件存在。但当我源代码时,我得到一个文件未找到错误 错误和日志: Running `target/debug/alias_cli h htop` . .alias Cargo.lock src .. .idea Cargo.toml target thread 'main' panicked at 'Failed to so

我正在使用命令板条箱来获取文件。我正在使用相同的命令行执行
ls-a
,然后再进行源代码生成。当我列出目录内容时,将要源文件存在。但当我源代码时,我得到一个文件未找到错误

错误和日志:

     Running `target/debug/alias_cli h htop`
.       .alias      Cargo.lock  src
..      .idea       Cargo.toml  target
thread 'main' panicked at 'Failed to source alias file: Os { code: 2, kind: NotFound, message: "No such file or directory" }', libcore/result.rs:1009:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.
./.alias%
fn source_alias_file(alias_file: &String) -> Result<(), Box<dyn Error>> {
    print!("{}", alias_file);
    Command::new("ls")
        .arg("-a")
        .spawn()
        .expect("Failed to list");

    Command::new("source")
        .arg(alias_file)
        .spawn()
        .expect("Failed to source alias file");

    Ok(())
}
代码:

     Running `target/debug/alias_cli h htop`
.       .alias      Cargo.lock  src
..      .idea       Cargo.toml  target
thread 'main' panicked at 'Failed to source alias file: Os { code: 2, kind: NotFound, message: "No such file or directory" }', libcore/result.rs:1009:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.
./.alias%
fn source_alias_file(alias_file: &String) -> Result<(), Box<dyn Error>> {
    print!("{}", alias_file);
    Command::new("ls")
        .arg("-a")
        .spawn()
        .expect("Failed to list");

    Command::new("source")
        .arg(alias_file)
        .spawn()
        .expect("Failed to source alias file");

    Ok(())
}
fn源别名文件(别名文件:&String)->结果{
打印!(“{}”,别名_文件);
命令::新建(“ls”)
.arg(“-a”)
.spawn()
.预期(“未能上市”);
命令::新建(“源”)
.arg(别名文件)
.spawn()
.expect(“未能生成别名文件”);
好(())
}
这里出了什么问题

我可以直接从终端获取文件,没有任何错误。

是一个bash命令,它不是一个程序,因此您不能像使用bash那样调用它:

Command::new("bash")
    .arg("-c")
    .arg(format!("source {}", alias_file))
    .spawn()
    .expect("Failed to source alias file");

这不会创建一个新的bash会话,该会话将在
命令
也将终止时立即终止。这将意味着只有在该会话中才能获取文件的来源。@leoOrion我不知道该命令的作用。是的,这样就可以了。我正在寻找一种方法,在我调用的
cargo run
from@leoOrion这在设计上是不可能的