New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explain &**node
#17
Comments
Thanks for logging the issue 👍 |
Just stumbled upon myself on this, I didn't even caught the new I also didn't fully understood was it was so obvious that we needed |
I was looking through this section, should the https://github.com/Gankro/too-many-lists/blame/master/text/second-iter.md#L259 Similar issue in the error at line 123123. https://github.com/Gankro/too-many-lists/blame/master/text/second-iter.md#L123 |
Remember what we want at
With @mackwic |
In keeping with the theme of the book, it probably would be good for the section to have another case of the compiler screaming at us for the mismatched type after adding |
I'm stil working through the book and always try to implement the current feature (e.g. pub struct List<T> {
head: Link<T>,
}
type Link<T> = Option<Box<Node<T>>>;
struct Node<T> {
elem: T,
next: Link<T>,
}
impl<T> List<T> {
// [...]
pub fn iter(&self) -> Iter<T> {
Iter(&self.head)
}
}
pub struct Iter<'a, T: 'a>(&'a Link<T>);
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
self.0.as_ref().map(|node| {
self.0 = &node.next;
&node.elem
})
}
} Not sure if this is any better or worse, but it gets rid of both the Edit: Unfortunately I'm struggling to use the same approach for |
It might be clearer to use |
I just published a major update and fixing this was one of the changes. (using Box::deref is interesting though) |
I tried @m-ou-se 's suggestion in playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7156f8fdd1d3c883379b5de868f230be Curious, I went to read the source code of impl<T: ?Sized> Deref for Box<T> {
type Target = T;
fn deref(&self) -> &T {
&**self
}
} I believe this can be introduced in the same vein as the pattern of Btw, the more general |
Hi, I was following the tutorial today and stumbled upon this chapter.. and my solution was the following: Iter { next: self.head.as_ref().map(|boxed_node| boxed_node.as_ref()) } I don't know whether people usually deref Actually I also just went ahead to read the source code of #[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
impl<T: ?Sized> AsRef<T> for Box<T> {
fn as_ref(&self) -> &T {
&**self
}
} I think either of |
From https://www.reddit.com/r/rust/comments/3geb5q/learning_rust_with_entirely_too_many_linked_lists/ctxgwmz, maybe explain
&**node
to make it more obvious what is being dereferenced. Coming from C, this was a little confusing.The text was updated successfully, but these errors were encountered: