On Sun, Apr 28, 2013 at 1:57 PM, Lee Braiden <[email protected]> wrote:
> On 28/04/13 18:45, Patrick Walton wrote:
>>
>> If you need to compare a `~str` against a constant string, use .equiv():
>>
>>     use core::cmp::Equiv;
>>
>>     fn main() {
>>         let x = ~"foo";
>>         if "foo".equiv(&x) {
>>             println("yep");
>>         }
>>     }
>>
>> This should admittedly be imported by default.
>
>
> Really?  Strings can't just be compared with == ?  To be honest, that alone
> is almost enough to put me off the language -- not only is it ugly and
> unwieldy, but it suggests a lot of limitations in the language's memory
> model / type system / operator overloading, which would also make my own
> code ugly and unwieldy.
>
> What's the problem with ==, or the difference with equiv(), exactly?  Is
> there some way to make it just work, no matter what kind of strings you're
> comparing?  Perhaps "foo" == (*x) would work, for example?
>
>
> --
> Lee

Strings and vectors are special cased in the syntax, ~"foo" and
~("foo") aren't the same type. This issue is specific to the
string/vector implementations included in the language, it's a
non-issue with borrowed pointers or owned/shared boxes.

Eq doesn't take a type parameter for the right-hand side parameter,
 so if ~"str" is on the left-hand side the right-hand side has to be
~"str" too. Although a ~str can be borrowed as a slice so "foo" ==
~"foo" *will* work.

Eq *could* take a type parameter like Add, Sub, etc. - it just doesn't
right now.
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to