Rust does not allow me to implement IntoIterator for Tree<T> :(

99 visualizzazioni
Passa al primo messaggio da leggere

Alessandro Stamatto

da leggere,
26 giu 2016, 16:41:0326/06/16
a CIS 198 - Rust - Public
I'm going through CIS 198 on my own pace (cool course!), and I'm stuck in Homework 03 - Iterators

I have a definition for Tree:
pub type Tree<T> = Option<Box<Node<T>>>;

An struct that represents the Iterator:
pub struct IntoIter<T> {
....//omitted
}

A working implementation for it:
impl<T> Iterator for IntoIter<T> {
....//omitted
}

But Rust does not allow me to implement IntoIterator for Tree:
impl<T> IntoIterator for Tree<T> { //RUST SHOUTS AT ME IN THIS LINE
....//omitted, but it should work...
}

And I get horrible error messages on that line:
"error: type parameter `T` must be used as the type parameter for some local type (e.g. `MyStruct<T>`); only traits defined in the current crate can be implemented for a type parameter [E0210]..."

I read the explanation suggested by the compiler, it seems it wants me to wrap Option<Box<Node<T>>> into a Tuple Struct, instead of using a Type Alias. Thing is... won't the code get a lot messier if I do that? (having to do mystruct.0 all the time?)

Sincerely, Alessandro Stamatto.

Kai Ninomiya

da leggere,
29 giu 2016, 12:57:0229/06/16
a Alessandro Stamatto, CIS 198 - Rust - Public

Alessandro,

Sorry for the slow response. Glad to hear you're using our course! We'd love for you to send us any feedback. I have not double checked out solutions recently, but I believe yours was the best approach we had:

> I read the explanation suggested by the compiler, it seems it wants me to wrap Option<Box<Node<T>>> into a Tuple Struct, instead of using a Type Alias.

The code fortunately does not get _too_ messy, just a few ".0"s scattered about.

Best,
-Kai


--
You received this message because you are subscribed to the Google Groups "CIS 198 - Rust - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cis198-publi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cis198-public/acd72c5a-dbe7-4fc1-bf27-11c442c42855%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alessandro Stamatto

da leggere,
29 giu 2016, 19:01:3529/06/16
a Kai Ninomiya, CIS 198 - Rust - Public
Thanks Kai! I want to do the entire course, but I'm doing in a slow pace...

I managed to implement the iterators (the reference ones were extremely hard for me, a mind twist for sure) that just "run through the right edges". 

Now I want to implement the "real" ones: iterating through in-order traversal. I'm very afraid of lifetime rules. The classical way to implement a tree iterator without recursion is using a stack. I don't know if Rust will let me have a vec (will be the stack) of references to nodes, will Rust believe in me that all the nodes will outlive my vec/stack? We'll see...

Alessandro Stamatto.

Kai Ninomiya

da leggere,
30 giu 2016, 19:39:2930/06/16
a Alessandro Stamatto, CIS 198 - Rust - Public
I think the full traversal turns out to be quite difficult (and may require concepts not covered yet) - we had maybe one student who did it, but I recommend moving on to the later assignments which should teach the necessary concepts better.

Alessandro Stamatto

da leggere,
1 lug 2016, 12:49:5101/07/16
a Kai Ninomiya, CIS 198 - Rust - Public
I'll move on, and come back when I finish the course. 

PS: the in-order traversal being hard to implement motivates me to do it, akin to a puzzle. 

Great course! I'm having fun along the way 😄
Rispondi a tutti
Rispondi all'autore
Inoltra
0 nuovi messaggi