3 ms·
In my little rust experience the suggestions the error messages had, even in the longer explanations, were useless. Mostly it came down to me trying to do some
by justinsaccount 10y ago
In my little rust experience the suggestions the error messages had, even in the longer explanations, were useless. Mostly it came down to me trying to do something that was simply not supported, but the compiler not knowing that and leading me on a wild goose chase.
From what I remember, I was trying to use the iterator trait, but return records that contained str pointers.. The goal being to parse a file and yield pointers to the strings from each line to avoid allocating memory and copying bytes around. Rust tries to tell you that you need lifetime specifiers, but if you try adding those nothing compiles anyway because the iterator stuff doesn't actually support that.
I eventually got it to work by returning copies of the strings.. maybe the unsafe stuff would have done what I wanted, that's what rust-csv seems to do at least.
I concluded that Rust is definitely not a language you can learn by chasing down error messages and incrementally fixing things. If you don't 100% understand the borrow and lifetime stuff, fixing one error is just going to cause two new ones.
- steveklabnik 10y agoHm, iterators can definitely return pointers. Post on users.rust-lang.org or drop by IRC sometime! We're happy to help.
- justinsaccount 10y agoOh? Maybe I was looking up the wrong thing. Now that I search again I think this is what I was trying to do: http://stackoverflow.com/questions/30422177/lifetime-in-iterator-impl http://stackoverflow.com/questions/30422177/lifetime-in-iter... though in my case I had a vec of &str or something like that. What I do remember was the compiler sending me in circles. Honestly if it had just said something like "Hey dummy, you can't do that, go read the chapters on lifetimes again" I might have fixed it. But it kept telling me things like "Just add a lifetime specifier on this line" and I would do that and then get 2 more error messages. I think the errors sent me down the path of trying to do what the SO post mentions, which is fn next<'a>(&'a mut self) -> Option<Vec<&'a T>> instead of putting the lifetime on the impl block, which probably never even occurred to me was something you could do.
- steveklabnik 10y agoAh ha, yes, this is more than just pointers. It's a "streaming" iterator as opposed to just an iterator, and yeah, we need more types, IIRC, before that's possible. As I mentioned below, we love bugs about bad error messages, feel free to file them!
- ckarmann 10y agoWell, if you love this kind of bug reports, please take a look at the one I filed a while ago :) https://github.com/rust-lang/rust/issues/11724 https://github.com/rust-lang/rust/issues/11724 Thanks!
- nercury 10y agoThe idea here is to separate lifetime of iteration (the scope in which "next" is used 'i) and the lifetime of data ('a), which would result in following signature: fn next<'i>(&'i mut self) -> Option<Vec<&'a T>> which, thanks to lifetime elision, is the same as fn next(&mut self) -> Option<Vec<&'a T>>