Skip to content

Instantly share code, notes, and snippets.

@sinsoku
Created March 26, 2020 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinsoku/51c9520bc784e33313a5a9b2bff25d39 to your computer and use it in GitHub Desktop.
Save sinsoku/51c9520bc784e33313a5a9b2bff25d39 to your computer and use it in GitHub Desktop.
I cannot build main.rs that includes `Option<R: BufRead>`. orz
use std::io::{self, Empty, BufRead, Write, Cursor};
fn read_input<R, W>(source: Option<R>, mut writer: W) -> String
where R: BufRead, W: Write
{
write!(&mut writer, "input: ").unwrap();
writer.flush().unwrap();
let mut input = String::new();
match source {
Some(mut reader) => {
reader.read_line(&mut input).unwrap();
},
None => {
io::stdin().read_line(&mut input).unwrap();
}
}
input.trim().to_string()
}
fn foo<R, W>(source: Option<R>, mut writer: W)
where R: BufRead, W: Write
{
let s1 = read_input(source, &mut writer);
let s2 = read_input(source, &mut writer);
println!("{}", s1);
println!("{}", s2);
}
fn main() {
let s1 = read_input(None::<Empty>, io::stdout());
let s2 = read_input(None::<Empty>, io::stdout());
let s3 = read_input(Some(Cursor::new(b"foo")), io::stdout());
println!("\n");
println!("{}", s1);
println!("{}", s2);
println!("{}", s3);
println!("----");
foo(None::<Empty>, io::stdout());
foo(Some(Cursor::new(b"bar")), io::stdout());
}
// --> src/main.rs:25:25
// |
// 21 | fn foo<R, W>(source: Option<R>, mut writer: W)
// | ------ move occurs because `source` has type `std::option::Option<R>`, which does not implement the `Copy` trait
// ...
// 24 | let s1 = read_input(source, &mut writer);
// | ------ value moved here
// 25 | let s2 = read_input(source, &mut writer);
// | ^^^^^^ value used here after move
//
// error: aborting due to previous error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment