When the following code is run it gives error:
<anon>:9:5: 9:25 error: a type named `Write` has already been imported in this module [E0251]
<anon>:9 use std::io::prelude::*;
If all the code under each of the "TRY RECURSIVE MACROS" comments (PARTS 1, 2 and 3) is removed, then all the "TRY ERRORS" code compiles successfully.
Conversely, if all the code under each of the "TRY ERRORS" comments (PARTS 1, 2 and 3) is removed, then all the "TRY RECURSIVE MACROS" code compiles successfully.
I want it to compile successfully with both the try_recursive_macros() function and the try_errors() function included.
use std::old_io;
// START - TRY RECURSIVE MACROS PART 1 OF 3
use std::fmt::Write;
// END - TRY RECURSIVE MACROS PART 1 OF 3
// START - TRY ERRORS PART 1 OF 3
use std::fs::File;
use std::io;
use std::io::prelude::*;
// END - TRY ERRORS PART 1 OF 3
fn main() {
// START - TRY RECURSIVE MACROS PART 2 OF 3
try_recursive_macros();
// END - TRY RECURSIVE MACROS PART 2 OF 3
// START - TRY ERRORS PART 2 OF 3
try_errors();
// END - TRY ERRORS PART 2 OF 3
}
// START - TRY RECURSIVE MACROS PART 3 OF 3
fn try_recursive_macros() {
macro_rules! write_html {
($w:expr, ) => (());
($w:expr, $e:tt) => (write!($w, "{}", $e));
($w:expr, $tag:ident [ $($inner:tt)* ] $($rest:tt)*) => {{
write!($w, "<{}>", stringify!($tag));
write_html!($w, $($inner)*);
write!($w, "</{}>", stringify!($tag));
write_html!($w, $($rest)*);
}};
}
let mut out = String::new();
write_html!(&mut out,
html[
head[title["Macros heading"]]
body[h1["Macros body text"]]
]);
assert_eq!(out,
"<html><head><title>Macros heading</title></head>\
<body><h1>Macros body text</h1></body></html>");
println!("HTML output is: {}", out);
}
// END - TRY RECURSIVE MACROS PART 3 OF 3
// START - TRY ERRORS PART 3 OF 3
fn try_errors() {
struct Info {
name: String,
age: i32,
rating: i32,
}
fn write_info(info: &Info) -> io::Result<()> {
let mut file = try!(File::open("my_fake_file.txt"));
try!(writeln!(&mut file, "name: {}", info.name));
try!(writeln!(&mut file, "age: {}", info.age));
try!(writeln!(&mut file, "rating: {}", info.rating));
return Ok(());
}
}
// END - TRY ERRORS PART 3 OF 3