I am trying to understand Rust polymorphism. From my background in OOP, I would expect the following Rust code to work:
use std::io::{stdin, Read};
fn main() {
let r: Read = stdin();
println!("ok");
}
But it doesn't:
4 | let r: Read = stdin();
| ^^^^^^^ expected trait std::io::Read, found struct `std::io::Stdin`
I know that there's a Read
impl for StdIn
, so how can I make this (or whatever is the correct way to do this) work, ie. use Stdin
, or a File
, or even a String
if possible (couldn't find a implementation for that) be used where a Read
is expected?
I don't think I can use generics here as I need to pass an instance of r
, which can be whatever that implements Read
, to another method later, but please tell me if I am wrong.