How to do it...

  1. Create a Rust project to work on during this chapter with cargo new chapter-ten.
  2. Navigate into the newly-created chapter-ten folder. For the rest of this chapter, we will assume that your command line is currently in this directory.
  3. Delete the generated lib.rs file, as we are not creating a library.
  4. Inside the src folder, create a new folder called bin.
  5. In the src/bin folder, create a file called inclusive_range.rs.
  6. Add the following code, and run it with cargo run --bin inclusive_range:
1   #![feature(inclusive_range_syntax)]
2   
3   fn main() {
4     // Retrieve the entire alphabet in lower and uppercase:
5     let alphabet: Vec<_> = (b'A' .. b'z' + 1) // Start as u8
6       .map(|c| c as char)      // Convert all to chars
7       .filter(|c| c.is_alphabetic()) // Filter only alphabetic     
chars 8 .collect(); // Collect as Vec 9 println!("alphabet: {:?}", alphabet); 10 11 // Do the same, but using the inclusive range syntax: 12 let alphabet: Vec<_> = (b'A' ..= b'z') // Start as u8 13 .map(|c| c as char) // Convert all to chars 14 .filter(|c| c.is_alphabetic()) // Filter only alphabetic
chars 15 .collect(); // Collect as Vec 16 println!("alphabet: {:?}", alphabet); 17 }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.195.29