The following code works:
use std::io;
use std::io::{BufReader, Read, Write};
use std::fs::File;
fn rc4(key: &[u8], input: &mut Read, output: &mut Write) {
}
fn main() {
let mut input: Box<Read> = match Some("file-from-parser.txt") {
Some(ifname) => Box::new(BufReader::new(File::open(ifname).unwrap())),
None => Box::new(io::stdin()),
};
rc4(&[1u8, 2, 3], &mut input, &mut io::stdout());
}
, borrow_mut(), Box , . , , rc4 :
fn rc4<R: Read, W: Write>(key: &[u8], input: &mut R, output: &mut W)
fn rc4<R: Read, W: Write>(key: &[u8], input: R, output: W)
( , &mut R Read, R - Read, Write; , , mut input: R / mut output: W API, )
.
, borrow_mut() , . , . , - , Box<Read> - -.
, Box :
fn main() {
let mut i1;
let mut i2;
let mut input: &mut Read = match Some("file-from-parser.txt") {
Some(ifname) => {
i1 = BufReader::new(File::open(ifname).unwrap());
&mut i1
}
None => {
i2 = io::stdin();
&mut i2
}
};
rc4(&[1, 2, 3], input, &mut io::stdout());
}
.