Skip to main content

print_tree_safe

Function print_tree_safe 

Source
pub fn print_tree_safe(
    tree: &BTreeMap<String, Box<Tree>>,
    depth: usize,
    max_depth: usize,
) -> Result<(), String>
Expand description

Prints the tree structure with indentation based on depth (SAFE RECURSIVE VERSION)

This version prevents stack overflow by enforcing a maximum recursion depth limit. If the depth exceeds the limit, an error is returned instead of crashing. The depth check costs less than the measurement noise (README, section Benchmarks).

§Arguments

  • tree - The tree to print
  • depth - Current depth level for indentation
  • max_depth - Maximum allowed recursion depth (typical: 1000-5000)

§Returns

  • Ok(()) if printing succeeded
  • Err(String) if maximum depth exceeded

§Example

use dir_path_2_tree::*;
let tree = Tree::default();
match print_tree_safe(&tree.0, 0, 1000) {
    Ok(()) => println!("Successfully printed tree"),
    Err(e) => eprintln!("Error: {}", e),
}