Logo

Programming-Idioms

History of Idiom 2 > diff from v100 to v101

Edit summary for version 101 by the10thwiz:
[Rust] Adjust the wording of the exlination to better explain why and how the underscore is used.

Version 100

2019-10-02, 09:48:43

Version 101

2019-10-02, 14:04:14

Idiom #2 Print Hello 10 times

Loop to execute some code a constant number of times

Illustration

Idiom #2 Print Hello 10 times

Loop to execute some code a constant number of times

Illustration
Code
for _ in 0..10 { println!("Hello"); }
Code
for _ in 0..10 { println!("Hello"); }
Comments bubble
0..10 syntax creates range iterator.

You can leave out variable name with underscore if you do not want to use it.
Comments bubble
0..10 syntax creates range iterator.

The compiler doesn't report unused variables prefixed with an underscore.
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20for%20_%20in%200..10%20%7B%0A%20%20%20%20%20%20%20%20println!(%22Hello%22)%3B%0A%20%20%20%20%7D%0A%7D&version=stable
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20for%20_%20in%200..10%20%7B%0A%20%20%20%20%20%20%20%20println!(%22Hello%22)%3B%0A%20%20%20%20%7D%0A%7D&version=stable