Iterating Over Strings

The following methods describe three different ways of traversing a String:

  • Tokenizing a String Object A String object can be tokenized on whitespace or a character token.

Tokenizing to Separate on Whitespaces

split_whitespace is used to split a String on the occurrence of whitespace. Loop through the String to split on whitespaces using a for loop.

Syntax

The general syntax is:

  for found in  str.split_whitespace(){
    println!("{}", found);
}
  

Here str is the original String which is to be traversed, split_whitespace() is a built-in keyword to split a string on whitespaces, for is used to traverse over the String and print it as soon as the whitespace is found and found is an iterator over the String.

  fn main() {
  // define a String object
  let str = String::from("Rust Programming"); 
  // split on whitespace
  for token in str.split_whitespace(){
      println!("{}", token);
  }
}
  

output

  Rust
Programming
  

Tokenizing to Split on a Custom Character

split method is used to split a sentence on some token. The token is specified in the split method. This would be useful to process comma-separated data, which is a common programming task.

  • Syntax

The general syntax is:

  for found in str.split(","){
    println!("{}", found);
}
  

Here str is the original String which is to be traversed,str.split() is a built-in method which takes a parameter, i.e., any delimiter and split the sentence on that parameter, for is used to traverse over the String and print a word before the token.

  fn main() {
  // define a String object
  let str = String::from("Educative, course on, Rust, Programming");  
  // split on token
  for token in str.split(","){
      println!("{}", token);
  }
}
  

output

  Educative
 course on
 Rust
 Programming
  

Iterating Over the String Object

chars method allows iterating over each element in a String using a for loop.

  • Syntax The general syntax is:
  for found in  str.chars(){
    println!("{}", found);
}
  

Here str is the original String which is to be traversed, str.chars() is a built-in keyword to denote letters in a String, for is used to traverse over the String and print every literal, and found is an iterator over the String.

  fn main() {
  // define a String object
  let str = String::from("Rust Programming");  
  // split on literal
  for token in str.chars(){
      println!("{}", token);
  }
}
  

output

  R
u
s
t
 
P
r
o
g
r
a
m
m
i
n
g
  

Quiz

Test your understanding on looping through the String object.

--- primaryColor: steelblue secondaryColor: '#e8e8e8' textColor: black shuffleQuestions: false shuffleAnswers: true locale: en --- # What is the output of the following code? ``` fn main() { // define a String object let str = String::from("RustLabs, course on, Rust; Programming"); // split on token for token in str.split(";") { println!("{}", token); } } ``` - [ ] ``` Rustlabs, course on, Rust Programming ``` - [ ] ``` RustLabs course on Rust Programming ```

Last updated 25 Jan 2024, 05:11 +0530 . history