Iteration

This page is under construction. Please come back later.
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#[macro_use]
mod utils;
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
let mut max:isize = 5;
if args.len() == 2 {
max = args[1].trim().parse::<i32>().unwrap_or(5);
}
{
let mut _i:isize = 0;
while _i < max {
println!("Hello, world!");
_i += 1;
}
}
}
Output
$ rustc Iteration1.rs
error[E0425]: cannot find value `Utils` in this scope
--> Iteration1.rs:8:9
|
8 | max = Utils.stoiWithDefault(args[1], 5);
| ^^^^^ not found in this scope
error: aborting due to previous error
For more information about this error, try `rustc --explain E0425`.
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
fn main() {
{
let mut i:isize = 10;
while i >= 1 {
println!("{}", i);
i += -1;
}
}
println!("Blastoff!");
}
Output
$ rustc Iteration2.rs
$ ./Iteration2
10
9
8
7
6
5
4
3
2
1
Blastoff!
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
fn main() {
println!("Even numbers up to 100...");
{
let mut i:isize = 2;
while i <= 100 {
println!("{}", i);
i += 2;
}
}
}
Output
$ rustc Iteration3.rs
$ ./Iteration3
Even numbers up to 100...
2
4
6
8
10
12
14
16
18
...
82
84
86
88
90
92
94
96
98
100
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
fn main() {
println!("Floating point numbers");
{
let mut d:f64 = 1.0f64;
while d < 2.01f64 {
println!("{}", d);
d += 0.1f64;
}
}
}
Output
$ rustc Iteration4.rs
$ ./Iteration4
Floating point numbers
1
1.1
1.2000000000000002
1.3000000000000003
1.4000000000000004
1.5000000000000004
1.6000000000000005
1.7000000000000006
1.8000000000000007
1.9000000000000008
2.000000000000001
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
fn main() {
println!("Powers of 2 up to 256!");
{
let mut i:isize = 1;
while i <= 256 {
println!("{}", i);
i *= 2;
}
}
}
Output
$ rustc Iteration5.rs
$ ./Iteration5
Powers of 2 up to 256!
1
2
4
8
16
32
64
128
256
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
fn main() {
let mut s:&'static str = "Hello, world! 🥰🇺🇸";
for c in s.chars() {
println!("{}", c);
}
}
Output
$ rustc Iteration6.rs
warning: variable does not need to be mutable
--> Iteration6.rs:2:6
|
2 | let mut s:&str = "Hello, world! 🥰🇺🇸";
| ----^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` on by default
warning: 1 warning emitted
$ ./Iteration6
H
e
l
l
o
,
w
o
r
l
d
!
🥰
🇺
🇸
Iterators
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Amortization Table
- Amortization Table (Revisited)
- Blackboard
- Blackboard (Revisited)
- Character Types
- Compute π (Monte Carlo)
- Floating Point Error
- ISBN-10 Check
- License Plate Generator
- Multiplication Table
- Number Guessing Game
- Punchcard Message
- Random Floats
- Random Integers
- Supermarket Sale
- Supermarket Sale (Revisited)
- Temperature Table
- Unicode Test
References
- [[Rust Language Reference]]
- [[Rust Compiler]]
Pure Programmer


