Skip to content

How can I compare string..? #3463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Sep 12, 2012 · 7 comments
Closed

How can I compare string..? #3463

ghost opened this issue Sep 12, 2012 · 7 comments
Assignees
Labels
C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Milestone

Comments

@ghost
Copy link

ghost commented Sep 12, 2012

1 import io::;
2 import core::
;
3 fn main(){
4 let price;
5
6 let item = io::stdin().read_line();
7
8 if str::eq(item,"salad"){
9 price = 3.5;
10 }else if str::eq(item,"kiwie"){
11 price = 2.25;
12 }else{
13 price = 5.0;
14 }
15
16 io::println(#fmt("%f",price));
17 }

I used to develope with C but there is a function "strcmp" but here. I don't know which I can use comparing the string..

Let me know them how to use.

Also I'd like to know about how to use API document properly. I can find and see the function and some kind of that. But I don't know how to use exactly

@ghost
Copy link
Author

ghost commented Sep 12, 2012

I think we can't compare the words which are static or constants.

That's the problem why I can't compare :)

@ghost ghost closed this as completed Sep 12, 2012
@ghost ghost reopened this Sep 12, 2012
@jdm
Copy link
Contributor

jdm commented Sep 12, 2012

You probably need to compare against ~"unique strings"

@AngryLawyer
Copy link

You can use == to compare strings, and as jdm says, you need the tilde in front of the strings

    if item == ~"salad" {
        price = 3.5;
    } else if item == ~"kiwie" {
         price = 2.25;
    } else {
        price = 5.0;
    }

Should make it compile, although I'm working off of the Master branch rather than the 0.3 release (and in mine, I have to cast the result of io::stdin() to io::ReaderUtil to get at read_line()

@graydon
Copy link
Contributor

graydon commented Sep 12, 2012

We're working on a number of bugs in this space; you're sort of stumbling over them all at once, sadly. We hope this will be cleaned up soon. It should not generally be necessary; in the meantime, str::cmp_slice will do what you're after (borrow both strings and compare them).

@nikomatsakis
Copy link
Contributor

I don't understand what the... bug is here, exactly? Is this a dup of #3470?

@nikomatsakis
Copy link
Contributor

No clear bug, far as I know things are working, closing.

@nikomatsakis
Copy link
Contributor

For posterity's sake: the reason that we can't just implement equality for &str and let the compiler handle the rest is that this wouldn't work for cases where you have strings embedded in data structures. For example, imagine comparing (~str, ~str)---in this case, we would match against the types (A, B) where A and B must implement Eq Here A (and B) are ~str, so ~str must implement Eq. This is in generic code so borrowing doesn't apply, since the code in question doesn't know if the value being compared is a ~str (which could be borrowed) or something else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

4 participants