Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Help. AI has out smarted me!
I know questions about AI generated code are frowned upon here but this question is not really about the AI.
I was busy building a doubly linked list that adhered to the rules of Crust Introducing Crust. Like C/C++ but C/Rust. Well OK, I my new AI friend to do it for me... All was going well, the code looked good, like a linked list in C. The tests all passed.
The I thought it would be nice to iterate over the list with for..in..
. Well, I thought that might be impossible because the Iterator trait uses references which breaks the rules of Crust. Or at least too complicate be reasonable. But once again my AI friend managed it, it does not look so complex, and it works!
BUT.. The AI has introduced a _marker: PhantomData<&'a T>,
field into the Iter
struct and sprinkled a bunch of `'around the place.
pub struct Iter<'a, T> {
current: *mut Node<T>,
_marker: PhantomData<&'a T>,
}
All of a sudden I don't understand what the AI has generated!
As far as vaguely understand what it is doing is taking care of the fact that the current
field is a raw pointer which has no lifetime information associated with it. So a marker filed is introduced which is a reference with a lifetime. And that lifetime can be checked in the Iterator
. Quite how all this changes together is a bit murky for me.
When I ask my AI friend how it works it goes on about "Variance" and "Drop Check" which is not helping.
Anyone have simple explanation of this PhantomData thing?
3 posts - 3 participants
🏷️ rust_feed