Expressions and starting flow control in RBE
This commit is contained in:
parent
505e04613f
commit
7204f08177
6
rustbyexample/.gitignore
vendored
6
rustbyexample/.gitignore
vendored
@ -1,6 +1,6 @@
|
|||||||
/*.d/*
|
*.d/out
|
||||||
|
*.d/**/out
|
||||||
|
expression
|
||||||
hello
|
hello
|
||||||
primitives
|
primitives
|
||||||
variable_bindings
|
variable_bindings
|
||||||
|
|
||||||
!/*.d/*.rs
|
|
||||||
|
25
rustbyexample/expression.rs
Normal file
25
rustbyexample/expression.rs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#[allow(path_statements)]
|
||||||
|
#[allow(unused_must_use)]
|
||||||
|
fn main() {
|
||||||
|
let x = 5;
|
||||||
|
x;
|
||||||
|
x + 1;
|
||||||
|
15;
|
||||||
|
|
||||||
|
let x = 5u32;
|
||||||
|
|
||||||
|
let y = {
|
||||||
|
let x_squared = x * x;
|
||||||
|
let x_cube = x_squared * x;
|
||||||
|
|
||||||
|
x_cube + x_squared + x
|
||||||
|
};
|
||||||
|
|
||||||
|
let z = {
|
||||||
|
2 * x;
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("x is {:?}", x);
|
||||||
|
println!("y is {:?}", y);
|
||||||
|
println!("z is {:?}", z);
|
||||||
|
}
|
23
rustbyexample/flow_control.d/if_else.rs
Normal file
23
rustbyexample/flow_control.d/if_else.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
fn main() {
|
||||||
|
let n = 5;
|
||||||
|
|
||||||
|
if n < 0 {
|
||||||
|
print!("{} is negative", n);
|
||||||
|
} else if n > 0 {
|
||||||
|
print!("{} is positive", n);
|
||||||
|
} else {
|
||||||
|
print!("{} is zero", n);
|
||||||
|
}
|
||||||
|
|
||||||
|
let big_n = if n < 10 && n > -10 {
|
||||||
|
println!(", and is a small number, increase net-fold");
|
||||||
|
|
||||||
|
10 * n
|
||||||
|
} else {
|
||||||
|
println!(", and is a big number, halve the number");
|
||||||
|
|
||||||
|
n / 2
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("{} -> {}", n, big_n);
|
||||||
|
}
|
18
rustbyexample/flow_control.d/loop.d/nested.rs
Normal file
18
rustbyexample/flow_control.d/loop.d/nested.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#![allow(unreachable_code)]
|
||||||
|
#![allow(unused_labels)]
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
'outer: loop {
|
||||||
|
println!("Entered the outer loop");
|
||||||
|
|
||||||
|
'inner: loop {
|
||||||
|
println!("Entered the inner loop");
|
||||||
|
|
||||||
|
break 'outer;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("This point will never be reached");
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Exited the outer loop");
|
||||||
|
}
|
13
rustbyexample/flow_control.d/loop.d/return.rs
Normal file
13
rustbyexample/flow_control.d/loop.d/return.rs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
fn main() {
|
||||||
|
let mut counter = 0;
|
||||||
|
|
||||||
|
let result = loop {
|
||||||
|
counter += 1;
|
||||||
|
|
||||||
|
if counter == 10 {
|
||||||
|
break counter * 2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(result, 20);
|
||||||
|
}
|
23
rustbyexample/flow_control.d/loop.rs
Normal file
23
rustbyexample/flow_control.d/loop.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
fn main() {
|
||||||
|
let mut count = 0u32;
|
||||||
|
|
||||||
|
println!("Let's count until infinity!");
|
||||||
|
|
||||||
|
loop {
|
||||||
|
count += 1;
|
||||||
|
|
||||||
|
if count == 3 {
|
||||||
|
println!("three");
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{}", count);
|
||||||
|
|
||||||
|
if count == 5 {
|
||||||
|
println!("OK, that's enough");
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
rustbyexample/flow_control.d/while.rs
Normal file
17
rustbyexample/flow_control.d/while.rs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
fn main() {
|
||||||
|
let mut n = 1;
|
||||||
|
|
||||||
|
while n < 101 {
|
||||||
|
if n % 15 == 0 {
|
||||||
|
println!("fizzbuzz");
|
||||||
|
} else if n % 3 == 0 {
|
||||||
|
println!("fizz");
|
||||||
|
} else if n % 5 == 0 {
|
||||||
|
println!("buzz");
|
||||||
|
} else {
|
||||||
|
println!("{}", n);
|
||||||
|
}
|
||||||
|
|
||||||
|
n += 1;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user