首页 > 解决方案 > Rust,特征绑定选项未使用 process::Command 定义

问题描述

首先,我知道有一个用于 git 的包,我正在制作一个程序来练习 Rust,我被堆叠起来了。这个想法(从现在开始)是查看当前分支并进行结帐(切换)。正如我所说,不要介意对实际分支进行结帐,并使用 Rust 进行练习。

这是一段代码:

fn get_current_branch() {
    let current_branch = Command::new("git")
        .arg("rev-parse")
        .arg("--abbrev-ref")
        .arg("HEAD")
        .output()
        .expect("Can't retrieve the current branch");
    let current: String = String::from_utf8(current_branch.stdout).unwrap().to_string();
    Command::new("git")
        .arg("checkout")
        .arg(current.pop())
        .spawn()
        .expect("Can't change branch");
}

问题是,在执行此操作时pop()显示错误:

“特征界限std::option::Option<char>: std::convert::AsRef<std::ffi::OsStr>不满足”

我查看了不同的可能答案,但没有任何效果。

感谢您的帮助,‍♂️。

标签: rust

解决方案


我假设您想使用整个分支,而不仅仅是第一个字符,这pop将给您。所以,只要使用current或者current.trim()如果你想摆脱之后有一个换行符。


推荐阅读