This content originally appeared on DEV Community 👩💻👨💻 and was authored by Antonov Mike
When I started learning Rust (2021.11.03), the first thing I encountered was printing a square and a triangle. I was confused at first. Then, when I understood how to write simple algorithms, I decided to write an algorithm for printing a Christmas tree. And in January 2022 I got this:
This is the algorithm
use std::thread;
use std::time::Duration;
fn main() { tannenbaum(7) }
fn tannenbaum(x: i32) {
let mut top = 0;
let mut i = 1;
while top < x/2 {
for _empty_space in 0..2 { print!(" ") }
top += 1
}
if x%2 == 1 { print!(" ") }
println!("A");
while i <= x {
for _empty_space in 0..(x-i) { print!(" ") }
for _left_side in 0..i { print!("L") }
for _middle in i..i+1 { print!("M") }
for _right_side in 0..i { print!("R") }
println!();
thread::sleep(Duration::from_millis(120));
i += 1
}
for _stem in 0..(x-1) { print!(" ") }
println!("ШШШ");
}
Try to colorize it using
[dependencies]
colored = "2"
Happy Holidays
This content originally appeared on DEV Community 👩💻👨💻 and was authored by Antonov Mike
Antonov Mike | Sciencx (2022-12-26T04:40:23+00:00) Algorithm for x-mass. Retrieved from https://www.scien.cx/2022/12/26/algorithm-for-x-mass/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.