To print a hollow pyramid pattern in Rust, you can use loops. The outer loop controls the rows, and the inner loops manage the spaces and stars to create the hollow effect.
In this example,
fn print_hollow_pyramid(rows: i32) {
for i in 1..=rows {
let mut pattern = String::new();
pattern += &" ".repeat((rows - i) as usize * 2);
for j in 1..=(2 * i - 1) {
if j == 1 || j == 2 * i - 1 || i == rows {
pattern += "* ";
} else {
pattern += " ";
}
}
println!("{}", pattern);
}
}
fn main() {
print_hollow_pyramid(5);
}
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Pyramid Pattern in Rust language with well detailed examples.