Here is an example of a for
loop in Rust that counts from 1 to 5:
fn main() {
for i in 1..=5 {
println!("{}", i);
}
}
This will print the numbers 1 through 5 to the console.
Here is an example of an if
statement in Rust that prints “positive” if a number is positive and “negative” if it is negative:
fn main() {
let num = 10;
if num > 0 {
println!("positive");
} else {
println!("negative");
}
}
This will print “positive” to the console.
And here is an example of a while
loop in Rust that counts from 1 to 5:
fn main() {
let mut i = 1;
while i <= 5 {
println!("{}", i);
i += 1;
}
}
This will also print the numbers 1 through 5 to the console.
Leave a Reply