In my little project all I need is to write either to a String or the stdout. How can I accomplish this without creating a version of if for each type? I was thinking in implementing a trait Writable that would do the trick but now it is poping alot of errors related of the trait must be dynamic and the size of object cannot be known at compile time:
tried that write in std::io but I was getting the same errors that i pointed below and i dont know if I can pass a String to it. Maybe if I can figure out whats going on down there maybe I can use it
The Playground above contains a complete code example that shows you exactly how to obtain a string: pass a &mut Vec<u8> which implements io::Write, and then convert it to a string or string slice.
You can't pass a String because it does not implement io::Write. (It implements fmt::Write but that doesn't help, because standard streams aren't fmt::Write, so there's no common trait. You could define your own, though.)
Hrm....it seems like you can't pass in a String directly. But as in my playground, you can pass in a Vec<u8>, and you can convert between that and String.
The one catch is that you cannot convert a String to a Vec<u8> and keep the original String around[1]. You're better off IMO using a Vec<u8> as your underlying storage, if you can, and then just converting it to a String when you need a String.