What's |_| in closure mean?

Hi, I am reading "Programming Rust", and there is |_|, does it mean one argument that I don't care? And can I write |_,_| ?

let mut router = BasicRouter::new();
router.add_route("/", |_| get_form_response());

Correct, it means one argument, and that you're not using it so you didn't give it a name. The _ name is exempt from various unused-symbol lints and warnings.

You can't omit the argument entirely because || ... and |_| ... have incompatible signatures. The former is specifically a zero-argument function; the latter is a one-argument function that happens not to use its argument. You can use |_, _| ... where you need a two-argument function and use neither argument, or combinations like |_, b| ... or |a, _| ... if you only use one of the two arguments.


If you used normal name, example item, compiler will give you a warning about unused variables.
You have two ways to shut this down:

  • #[warn(unused_variables)] annotation to ignore this kind of warning.
  • or starting the variable's name with underscore.

You can find more in this answer