[rust-dev] Syntax for custom type bounds

Vadim vadimcn at gmail.com
Thu Jan 30 14:58:14 PST 2014


Just wondering...  Does anyone else feel that "syntax" around lifetimes and
type bounds is becoming very unwieldy?  I am referring to this PR:
https://github.com/mozilla/rust/pull/11768.   Is using dummy struct fields
for specifying custom type bounds on the struct itself really the best we
can do in a brand-new language?


At least for the lifetimes, Rust could extend existing syntax and allow
specifying them on by-value returns objects, so that, for example, this:

    pub trait MutableVector<'a, T> {
        fn mut_iter(self) -> MutItems<'a, T>;
        ...
    }

could be written as:

    pub trait MutableVector<'a, T> {
        fn mut_iter(self) -> 'a MutItems<T>;
        ...
    }

This also makes the intent much more clear.   Currently, one would have to
dig into the definition of MutItems<'a,T> to figure out that the lifetime
parameter 'a is used to create a dummy borrow field into the vector, so
that the whole iterator is then treated as a mutable borrow.   This feels
very convoluted, if you ask me.


On a slightly different note, is there a strong reason for having to name
lifetime parameters explicitly?   Could we simply re-use actual parameter
names prefixed with ' as their lifetimes?   The above could then be reduced
to this:

    pub trait MutableVector<T> {
        fn mut_iter(self) -> 'self MutItems<T>;
        ...
    }

This used to be valid syntax, btw, though it worked because 'self lifetime
was special, IIRC.
Note that I am not proposing to eliminate explicitly named lifetimes
entirely,- there are still cases when they are needed.  But as far as I've
seen, those are few and far between.

Thanks for reading,
Vadim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140130/199b2751/attachment.html>


More information about the Rust-dev mailing list