samedi 29 mars 2014

How to iterate through characters in a string in Rust?


Vote count:

0




Just starting to play around with Rust. Starting with a basic example, iterating through a sentence to extract out simple words from the string.


Here's what I have so far, trying to make that parse function first match world in the input string:



fn parse(input: ~str) -> ~str {
let mut val = ~"";

for c in input.chars() {
if c == ~"w" { // guessing I have to test one character at a time
val.push_str(c.to_str());
}
}

return val;
}

fn main() {
let s: ~str = ~"Hello world!";
println!("{}", parse(s));
}


What is the correct way to iterate through the characters in a string to match patterns in Rust (such as for a basic parser)? Are there any open source repos that show a real-world example to do this too, that would be helpful as well.



asked 46 secs ago






Aucun commentaire:

Enregistrer un commentaire