For works, map not

Hi,

I was playing with tree and map...
I have this example that works when I use a for, but not with a map...
Why map not apply recursive function to print like with for please ?
I was thinking map more elegant... but I am surprised to see the difference...

#[derive(Debug)]
struct Node<T> {
    value: T,
    children: Vec<Node<T>>,
}

fn main() {
    let root = Node {value: "One", children: vec![]};
    let mut root = Node {value: 4, children: vec![]};
    root.children.push(Node {value:5, children: vec![]});
    root.children.push(Node {value:6, children: vec![]});
    println!("NODE  {:#?}", root);
    travel(&root);
}

fn travel<T: std::fmt::Debug>(node: &Node<T>){
    println!("node_P : {:#?}", node.value);

/*  This way works
 *  for c in node.children.iter() {
        travel(c);
    }
*/

    //This way only not
    node.children.iter().map(travel);
    //node.children.iter().map(|c| travel(c));
}

Read the warning that the compiler gives you, it explains the problem and gives a solution.

warning: `Iterator::map` call that discard the iterator's values
  --> src/main.rs:26:26
   |
16 | fn travel<T: std::fmt::Debug>(node: &Node<T>){
   | --------------------------------------------- this function returns `()`, which is likely not what you wanted
...
26 |     node.children.iter().map(travel);
   |                          ^^^^------^
   |                          |   |
   |                          |   called `Iterator::map` with callable that returns `()`
   |                          after this call to map, the resulting iterator is `impl Iterator<Item = ()>`, which means the only information carried by the iterator is the number of items
   |
   = note: `Iterator::map`, like many of the methods on `Iterator`, gets executed lazily, meaning that its effects won't be visible until it is iterated
   = note: `#[warn(map_unit_fn)]` on by default
help: you might have meant to use `Iterator::for_each`
   |
26 |     node.children.iter().for_each(travel);
   |                          ~~~~~~~~
6 Likes

Damn, I was focalized on the result...
I was thinking map give us each element in order to manipulate it.
I will continue to explore all functionalities.
Thank you for your time.