I am fine with == not working for strings. Equality is a bit of a bugger in
general and very few languages seem to have got it properly solved. It
would be fantastic, however, if match ... {} patterns worked as expected
(and, preferably efficiently).

Actually, grep-ing my own code (mostly scala) I can't actually find any
instances of  if (str == "foo"){} type code. It is mostly handled as match
statements, so maybe I am not the right person to talk.

It allocates, unfortunately, unless something has changed since I last
> looked at the code. This is unfortunate and should be changed. My preferred
> fix is to just get rid of ~"string" in patterns entirely, as it's ugly, and
> make "Foo" patterns match against any type of string you have, without
> allocating.
>
> Patrick
>
> Mitch Skinner <[email protected]> wrote:
>>
>> On Sun, Apr 28, 2013 at 10:45 AM, Patrick Walton <[email protected]>wrote:
>>
>>> If you need to compare a `~str` against a constant string, use .equiv():
>>>
>>
>>  I have some code where I'm trying to match an owned string against a
>> set of constant strings, and it's not clear to me how to take your advice
>> there.
>>
>> fn match_upper(to_match: &str) -> int {
>>     match to_match.to_ascii().to_upper().to_str_ascii() {
>>         ~"ABC" => 1, ~"DEF" => 2, ~"GHI" => 3, _ => 0
>>     }
>> }
>>
>> Does each call to this function heap-allocate all the owned strings in
>> the match expression?  If so, how could I avoid that?  Is there a more
>> idiomatic way to do what I'm trying to do?
>>
>> Thanks for the PSA,
>> Mitch
>>
>> ------------------------------
>>
>> Rust-dev mailing list
>> [email protected]
>> https://mail.mozilla.org/listinfo/rust-dev
>>
>>
> --
> Sent from my Android phone with K-9 Mail. Please excuse my brevity.
>
> _______________________________________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/rust-dev
>
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to