3

Here's an example of what I've tried.

static TARGET: &'static str = "a string";

fn main () {
  printfln!("%?", TARGET.eq(~"other string"));
}

I looked at equiv too, but no luck. The string I compare to the TARGET has to be an owned pointer string.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Johanna Larsson
  • 10,531
  • 6
  • 39
  • 50
  • 1
    All fmt! related macros changed BTW. Your code will break with the next update. Take a look at here: http://static.rust-lang.org/doc/master/std/fmt/index.html – Ercan Erden Oct 26 '13 at 00:13

1 Answers1

4

This works here:

static TARGET: &'static str = "a string";

fn main () {

  println!("{}", TARGET == "a string");
  println!("{}", TARGET == ~"a string");

  let other = ~"a string";
  println!("{}", TARGET == other);

}

It prints:

true
true
true
Ercan Erden
  • 1,808
  • 1
  • 11
  • 8
  • I'm sorry, I didn't make it clear that the string had to be a pointer. Edited the question. – Johanna Larsson Oct 26 '13 at 10:23
  • @ErikKronberg if `TARGET == ~"foo"` didn't work when you tried it, you should say the error message you got. (If it doesn't work, you can try forcing it to be a `&str` via `foo.as_slice()` where `foo` has type `~str`.) – huon Oct 26 '13 at 10:43
  • You are quite right, when I was able to check up on it, I got no error messages. I must have mixed it up with something else! – Johanna Larsson Oct 30 '13 at 15:43
  • I've been trying this out and found. `"a string" == other` works fine, but `other == "a string"` gives a type error! – Adrian Ratnapala Jan 25 '14 at 12:31
  • @Adrian Ratnapala That seems like an implementation issue at the moment. – Ercan Erden Jan 26 '14 at 18:55