Why is the trait `core :: fmt :: Show` not implemented for type` core :: fmt :: Show + Sized`?
I am trying to compile some code, but I am getting a rather strange error:
trait
core::fmt::Show
not implemented for typecore::fmt::Show + Sized
And the code:
use std::fmt::Show;
fn main() {
println!("{}", return_showed()); // Error occurs here
}
fn return_showed() -> Box<Show+Sized+'static> {
box "test" as Box<Show+Sized>
}
It doesn't really matter to me. Is this a compiler bug?
Thanks in advance!
source to share
Rust 1.0
The code posted in the original question compiles as expected.
Original
If you don't need it Sized
, you can use this:
fn show_boxed() -> Box<Show+'static> { // '
box "test"
}
fn main() {
println!("{}", &*show_boxed());
}
As I understand it &*
will dereference and then re-reference Box. This changes it from Box<core::fmt::Show>
to &core::fmt::Show
, which the formatter knows how to deal with.
Edit
You can also select only the object you want:
fn return_showed() -> Box<Show+Sized+'static> { // '
box "test" as Box<Show+Sized>
}
fn main() {
let z: &Show = &*return_showed();
println!("{}", z);
}
I agree that this is less than ideal; it might be worth writing down the rust problem.
source to share