To print a pyramid pattern in Rust, you can use nested loops where the outer loop controls the rows and the inner loops control the spaces and stars printed in each row.
In this example,
print_pyramid
that takes an integer n
as input to represent the number of rows in the pyramid.println!
to move to the next line.fn print_pyramid(n: usize) {
for i in 0..n {
print!("{}", " ".repeat(n - i - 1));
println!("{}", "*".repeat(2 * i + 1));
}
}
fn main() {
print_pyramid(5);
}
* *** ***** ******* *********
In this tutorial, we learned How to print Pyramid Pattern in Rust language with well detailed examples.