How to print text diagonally from left to right recursively in Rust

1 Answer

0 votes
fn print_text_diagonally_recursively_ltr(spaces: &str, text: &str) {
    if !text.is_empty() {
        // Print the first character with current indentation
        println!("{}{}", spaces, text.chars().next().unwrap());

        // Get the rest of the string safely (Unicode‑aware)
        let rest = text.chars().skip(1).collect::<String>();

        // Recursive call with one more space
        print_text_diagonally_recursively_ltr(&format!("{} ", spaces), &rest);
    }
}

fn main() {
    print_text_diagonally_recursively_ltr("", "HELLO WORLD");
}


/*
run:

H
 E
  L
   L
    O
      
      W
       O
        R
         L
          D

*/

 



answered Apr 8 by avibootz

Related questions

...