I don't know why this error happend.Can someone help ? I have gave up
⚓ Rust 📅 2025-10-09 👤 surdeus 👁️ 4This is a runtime
let command = Command::builder()
.executable("cargo".to_owned())
.args(vec!["build".to_owned(), "--release".to_owned()])
.env(vec![])
.build()
.unwrap();
assert!(command.current_dir.is_none());
let command = Command::builder()
.executable("cargo".to_owned())
.args(vec!["build".to_owned(), "--release".to_owned()])
.env(vec![])
.current_dir("..".to_owned())
.build()
.unwrap();
assert!(command.current_dir.is_some());
This is Implement:
pub struct Command {
executable: String,
args: Vec<String>,
env: Vec<String>,
current_dir: String,
}
struct CommandBuilder {
executable: std::option::Option<String>,
args: std::option::Option<Vec<String>>,
env: std::option::Option<Vec<String>>,
current_dir: std::option::Option<String>,
}
impl Command {
pub fn builder() -> CommandBuilder {
CommandBuilder {
executable: None,
args: None,
env: None,
current_dir: None,
}
}
}
impl CommandBuilder {
pub fn executable(&mut self, executable: String) -> &mut Self {
self.executable = Some(executable);
self
}
pub fn args(&mut self, args: Vec<String>) -> &mut Self {
self.args = Some(args);
self
}
pub fn env(&mut self, env: Vec<String>) -> &mut Self {
self.env = Some(env);
self
}
pub fn current_dir(&mut self, current_dir: String) -> &mut Self {
self.current_dir = Some(current_dir);
self
}
pub fn build(&mut self) -> Result<Command, Box<dyn std::error::Error>> {
Ok(Command {
executable: self.executable.clone().ok_or("")?,
args: self.args.clone().ok_or("")?,
env: self.env.clone().ok_or("")?,
current_dir: self.current_dir.clone().ok_or("")?,
})
}
}
this is a error
62 | current_dir: Option<String>,
| ^^^^^^^^^^^----------------
help: try wrapping the expression in `Some`
|
78 | .current_dir(Some("..".to_owned()))
| +++++ +
2 posts - 2 participants
🏷️ Rust_feed