Why a rust immutable reference can call &mut self methd?
in my code comments:
- stdin's type is &ChildStdin, why can call a "&mut self" function write_all?
- and why as_ref result can call a "&mut self" function write_all?
use std::io::{self, Write};
use std::process::{Command, Stdio};
fn main() -> io::Result<()> {
let child = Command::new("cmd.exe")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;
let mut stdin = child.stdin.as_ref().unwrap();
//1. stdin's type is &ChildStdin, why can call a "&mut self" function write_all?
stdin.write_all(b"dir\n")?;
//2. and why as_ref result can call a "&mut self" function write_all?
child.stdin.as_ref().unwrap().write_all(b"dir\n")?;
let output = child.wait_with_output()?;
println!("output = {:?}", output);
Ok(())
}