0

I wanted to verify substring existence in a string and came up with:
if "haystack".find("needle") != -1:

But I would have prefered: if "needle" in "haystack": as in python.
Though we get:

Error: type mismatch: got (string, string) but expected one of: proc contains[T](s: Slice[T]; value: T): bool proc contains[T](x: set[T]; y: T): bool proc contains[T](a: openArray[T]; item: T): bool

note that if you import strutils it starts to work.

v.oddou
  • 6,476
  • 3
  • 32
  • 63

1 Answers1

4

You already gave the answer yourself, import strutils to get the contains proc for strings. Nim automatically used the contains proc for the in operator.

def-
  • 5,275
  • 20
  • 18
  • 1
    Ok, just wanted a bit more depth out of it, because one would think, an operator is a language keyword. But in this case it is a proc that gets called after a remap of `in` to `contains` that's convoluted. Also I'm just posting tons of basic questions to make google useful for nim. Right now it's the desert, nim is unusable because of how google is unhelpful with basic questions. – v.oddou Feb 23 '18 at 03:38