v icon indicating copy to clipboard operation
v copied to clipboard

Dose for in not support string?

Open tonylinyy opened this issue 7 years ago • 5 comments

Dose for in not support string?

tonylinyy avatar Apr 23 '19 09:04 tonylinyy

Sorry, I don't understand the question.

medvednikov avatar Apr 23 '19 09:04 medvednikov

I think he means "can a for-loop iterate over the characters in a string?"

zac-garby avatar Apr 23 '19 09:04 zac-garby

@zac-garby yes,like this: for char in string { ... }

tonylinyy avatar Apr 23 '19 09:04 tonylinyy

Now you can:

fn main() {
        s := 'hello'
        for c in s {
                println(c.str())
        }
}

UTF-8 is not handled yet, but it will be like Go's range str soon.

medvednikov avatar Apr 23 '19 11:04 medvednikov

I run your code,here is the error message: for in requires an array but got string

tonylinyy avatar Apr 23 '19 12:04 tonylinyy