How to assign a the result of `Command::arg()` to a field?

30 Views Asked by At

tokio's arg() returns a mutable reference to a Command. How can I assign it to a field?

pub struct Manager<'a> {
    pub cmd: &'a mut tokio::process::Command
}

impl<'a> Manager<'a> {
    pub fn new() -> Manager<'a> {
        Manager {
            cmd: tokio::process::Command::new("ls").arg("la")
        }
    }
}

Error Message:

returns a value referencing data owned by the current function

1

There are 1 best solutions below

1
Chayim Friedman On BEST ANSWER

The method returns a reference to the same Command it was invoked on, just to make it easy to chain method calls (command.arg("abc").arg("def").spawn()). You may as well ignore its return value and just assign the Command to the field:

pub struct Manager {
    pub cmd: tokio::process::Command,
}

impl Manager {
    pub fn new() -> Manager {
        let mut cmd = tokio::process::Command::new("ls");
        cmd.arg("la");
        Manager { cmd }
    }
}