I am trying to build a function which takes standard input the same way C++
's std::cin >>
does, keeping the code as simple as possible.
use std::io::BufRead;
pub fn input<T: std::str::FromStr>(handle: &std::io::Stdin) -> Result<T, T::Err> {
let mut x = String::new();
let mut guard = handle.lock();
loop {
let mut available = guard.fill_buf().unwrap();
match available.iter().position(|&b| !(b as char).is_whitespace()) {
Some(i) => {
available = &available[i..];
guard.consume(i);
}
None => {
guard.consume(available.len());
continue;
}
}
match available.iter().position(|&b| (b as char).is_whitespace()) {
Some(i) => unsafe {
available = &available[..i];
guard.consume(i);
x.as_mut_vec().extend_from_slice(available);
break;
}
None => {
guard.consume(available.len());
continue;
}
}
}
T::from_str(&x)
}
The lint tells me that the StdinLock
has multiple mutable references, whereas I see only one, that is guard
. What am I missing?