Hi,
I'm new to Rust and am writing one of my first programs, but I'm familiar with c++.
Please can you let me know if there a way for me to deal with the error 'dyn Iterator<Item = &MyType>' doesn't implement 'Debug'
?
MyType
implements Debug
.
I have tried searching for this and tried various ways of addressing it but none worked. At one point I derived a new trait DebugIterator
, with supertraits of Iterator
and Debug
, and that led me stumbling to here (Tracking issue for RFC 2289, "Associated type bounds" · Issue #52662 · rust-lang/rust · GitHub) but I am not sure if it means my approach isn't going to work.
I'm not sure if further context would be useful or appreciated, but here is the background just in case the idiomatic solution is for an overhaul rather than fixing this specific error!
I am trying to write a series of trait implementations that will convert a number of types into fewer more generic types in a Enum (as part of a process towards serializing them and just to learn). I am aware of the serde crate, but I would like to write something custom myself to learn Rust, and inspecting the serde source is a bit beyond me.
For example, so far I have an enum MyType
and my process has been:
-
u32
,i32
(and suitable others) are returned asMyType::Int(i64)
; -
f32
(and suitable others) asMyType::Float(f32)
; -
String
(and perhaps suitable others) asMyType::String(&str)
; - and I'd like to return
Vec
,Hashmap
(and suitable others) as some form of reference to a generic collection.
My understanding is that the idiomatic way of representing what I am calling 'collection' classes is through their iterator, but the iterator of Vec
and Hashmap
are of different concrete types. I think the idiomatic way of representing these is thus a reference to &dyn Iterator<Item::&MyType>
through MyType::Iter(&dyn Iterator<Item::&MyType>)
[where Iter
is one of my MyType
enum variants] using the common Iterator trait.
The issue I have is that MyType
derives Debug
, but because 'dyn Iterator<Item = &MyType>' does not derive
Debug`, I get the error mentioned at the start.
I see from playing around that dyn Iterator<Item = &i32>
cannot be formatted using {:?}
because it doesn't implement Debug
, yet a normal iterator of i32 references obviously is Debug
, so is there a reason why trait objects cannot?
[Hopefully formatting is ok, this is hopefully my first post of many, so I welcome constructive criticism!]