I'm trying to understand why the third option below works. Here is my thought process (rust newbie) and assumptions (which might be incorrect).
- The first argument to
writeln!
must be a mutable reference. - With option 1,
to_child
is a mutable binding to an owned value, so Rust will take a mutable reference automatically because the binding was labeled asmut
. - With option 2,
to_child
is an immutable binding (nomut
keyword) to a mutable reference, so Rust doesn't do anything automatically as the variable is already a mutable reference. - With option 3,
to_child
is a mutable binding to an immutable reference, so Rust will change the immutable reference to a mutable reference because the binding has themut
keyword??? Is this why it works?
I think I'm overthinking this, but I've been reading answers about the differences between immutable/mutable bindings and immutable/mutable references, which clearly state they are all different from each other.
Any help would be appreciated!
use std::error::Error;
use std::io::prelude::*;
use std::process::{Command, Stdio};
fn main() -> Result<(), Box<dyn Error>> {
let mut child = Command::new("grep")
.arg("test")
.stdin(Stdio::piped())
.spawn()?;
// Option 1: Take ownership of ChildStdin with mutable binding
// let mut to_child = child.stdin.take().unwrap();
// Option 2: Take mutable reference to ChildStdin with immutable binding
// let to_child = child.stdin.as_mut().unwrap();
// Option 3: Take immutable reference to ChildStdin with mutable binding
let mut to_child = child.stdin.as_ref().unwrap();
writeln!(to_child, "this is a test")?;
drop(to_child);
child.wait()?;
Ok(())
}