I've just started off with Rust a month ago, and I'm finding it a fascinating language, but I've got a bit stuck on this:
I want to make a trait similar to the below structure (my particular use case is a bit more complex but this captures the issue and error I'm getting). The issue I have is which lifetimes in the last impl. Note all the impls themselves will compile as independent functions which I believe means with enough genericity I should be able to place them under one trait. How can I sort out the lifetimes (for both the trait and the impls) so this compiles?
You can't make that last impl because you would need to be able to name the local scope of the function to describe the borrow of x, but that isn't possible.
I don't really understand what you're getting at with that playground link.
I can compile the function as a non-trait function, with an actual, non-"unimplemented" implementation.
Are you saying despite that, there's some functions that I can't put inside a trait (that is, aside from making them have an undefined result), and this is one of them?
no the unsafe bit is f(&x) nothing else. The lifetime of the reference is shorter than 'a so you can't pass it in to f. So Rust rightly rejects it, and you haven't gotten away with it .
Also, the function you are showing now, and the one in the trait are different.