match
for core
Conditionally run a block on a matched value.
Signature
> match {flags} (value) (match_block)
Parameters
value
: Value to check.match_block
: Block to run if check succeeds.
Input/output types:
input | output |
---|---|
any | any |
Examples
Match on a value in range
> match 3 { 1..10 => 'yes!' }
yes!
Match on a field in a record
> match {a: 100} { {a: $my_value} => { $my_value } }
100
Match with a catch-all
> match 3 { 1 => { 'yes!' }, _ => { 'no!' } }
no!
Match against a list
> match [1, 2, 3] { [$a, $b, $c] => { $a + $b + $c }, _ => 0 }
6
Match against pipeline input
> {a: {b: 3}} | match $in {{a: { $b }} => ($b + 10) }
13
Match with a guard
> match [1 2 3] {
[$x, ..$y] if $x == 1 => { 'good list' },
_ => { 'not a very good list' }
}
good list
Notes
This command is a parser keyword. For details, check: https://www.nushell.sh/book/thinking_in_nu.html