4 ms·
I am new to Rust, but this: for<'a, T: ?Sized> &'a T !: DerefMut Seems like a hunk of unreadable code. Is this valid Rust? https://internals.rust-lang.o
by svnpenn 7y ago
I am new to Rust, but this:
for<'a, T: ?Sized> &'a T !: DerefMut
Seems like a hunk of unreadable code. Is this valid Rust?
https://internals.rust-lang.org/t/unsoundness-in-pin/11311/33 https://internals.rust-lang.org/t/unsoundness-in-pin/11311/3...
- kibwen 7y agoI think the `!` part might be a typo, and that pseudocode needs more context to be valid, but the individual elements are themselves valid even if you'd never see them composed in this way, and I wouldn't expect any regular user to have to ever read or write anything like this. The leading `for<>` indicates a higher-ranked lifetime (which I've never once needed in eight years of writing Rust (though to be fair, I think it's only existed for like six of those years...), the `!` is the "Never" type indicating a statically-impossible state (honestly it really should just be called `Never` rather than `!`, this is a leftover artifact of prehistoric versions of Rust that predate even me. Maybe I'll write an RFC to propose that), the `?Sized` is a one-of-a-kind trait bound that users might need rarely (but I don't bother with, generally), and the `'a` is a lifetime type parameter (which is common for anyone working with references, although it's such an important part of the language that you don't bat an eye at it after a while).
- dethinking 7y agoYes, it’s a type signature. It looks typical of Rust internals, but it would be very verbose for user code. You’re not wrong to find it illegible; apart from the syntax, that phrase invokes the concepts of lifetimes, mutability, borrowing, and traits, none of which have straightforward semantic analogues in other common languages. But FWIW they’re primitive concepts in Rust. A typical Rust user wouldn’t read or write code that looks like that very much, but they would know what it means, if that makes any sense. Edit: Seeing sibling comments about whether this snippet actually parses, I guess I should amend this to say that a typical Rust user has an idea of what it’s supposed to mean :)
- hollerith 7y agoSignature for the type of which identifier? "for"?
- dethinking 7y agoI think I originally had “piece of a type signature”, but I didn’t actually want to figure out which piece, so I fudged it. The point is well taken, I oughtn’t pretend I have a Rust parser in my head :)
- dodobirdlord 7y agoFirst of all, it’s not actually valid syntax because I believe Rust doesn’t have negative trait bounds. But it’s a trait bound, so it’s (part) of a type signature. The type here is generic over both lifetimes and types (I think). My vague understanding is that for<‘a, T: ?Sized> means “in any lifetime, any type that is a member of ‘?Sized’”. And then the rest of the signature goes on to say (in made-up syntax) “does not implement DerefMut”. I think ?Sized means “size not known at compile time.”
- Rusky 7y agoNo, it's bits of valid syntax thrown together informally to express an idea. There is no `!:` in Rust, and that `for` quantification never applies to types or bounds that way.
- withoutboats 7y ago(and the idea I was expressing was "shared references never under any circumstance implement the DerefMut trait")
- bascule 7y ago`for` used in a bound (in conjunction with a lifetime) is the syntax for Higher-Rank Trait Bounds (HRTB): https://doc.rust-lang.org/beta/nomicon/hrtb.html https://doc.rust-lang.org/beta/nomicon/hrtb.html
- Rusky 7y agoYes, but it's not used that way in this snippet...
- deleted 7y ago[deleted]
- staticassertion 7y agoI've been writing rust since just before 1.0 and I've never written a type signature like that. I understand it just fine (even though it is invalid, I get what they're saying), but yeah, never needed it. It's both a good and bad thing that rust has these sort of deep areas where 95% of users won't ever have to go - Pin being another example. I think it's much more good than bad though.