Originally, my struct only printed to stdout using println!. However, I now have the need for it to be able to print output to other places, such as Strings. How would I go about doing this?
In C++ for example, I'd have the struct store a reference to an ostream, because both strings and stdout inherit from it.
In Rust though, this is a little harder, particularly because of borrow checking.
I initially thought that I could store the target output using Box<dyn Write>, which I could then write to with writeln!. This works fine when I use std::io::stdout() as the output. But, when I try to put a writer for a Vec<u8>, it's problematic because I won't be able to read the characters later, because the struct now owns the writer.
How can I do this?