gpt4 book ai didi

Rust,特征绑定(bind)选项未使用 process::Command 定义

转载 作者:行者123 更新时间:2023-12-03 11:48:18 25 4
gpt4 key购买 nike

首先,我知道有一个用于 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() 时的行它显示一个错误:

"the trait bound std::option::Option<char>: std::convert::AsRef<std::ffi::OsStr> is not satisfied"


我查看了不同的可能答案,但没有任何效果。
感谢您的帮助,🙇‍♂️。

最佳答案

我假设你想使用整个分支,而不仅仅是第一个字符,这就是 pop会给你。所以,只需使用 currentcurrent.trim()如果之后有一个你想摆脱的换行符。

关于Rust,特征绑定(bind)选项未使用 process::Command 定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62847910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com