Matching
Examples in
V
// A match statement is a shorter way to write a sequence of if - else statements.
// When a matching branch is found, the following statement block will be run, and the final expression will be returned.
// The else branch will be evaluated when no other branches match.
os := 'windows'
print('V is running on ')
match os {
'darwin' { println('macOS.') }
'linux' { println('Linux.') }
else { println(os) }
}
number := 1
s := match number {
1 { 'one' }
2 { 'two' }
else {
println('this works too')
'many'
}
}
// A match statement can also be used to branch on the variants of an enum by using the shorthand .variant_here syntax.
enum Color {
red
blue
green
}
fn is_red_or_blue(c Color) bool {
return match c {
.red { true }
.blue { true }
else { false }
}
}
Last Run
:
================ V panic ================
module: builtin
function: get()
file: /home/ubuntu/code_runner/root_volume/v/vlib/builtin/array.v
line: 153
message: array.get: index out of range (i == -1, a.len == 0)
=========================================
| 0x56059535eb5a | /code/root/v/v(panic_debug+0x131)
| 0x56059535d740 | /code/root/v/v(array_get+0xaf)
| 0x5605953a71c3 | /code/root/v/v(compiler__CGen_insert_before+0x60)
| 0x5605953cc01b | /code/root/v/v(compiler__Parser_match_statement+0x1c4)
| 0x56059539477c | /code/root/v/v(compiler__Parser_statement+0x458)
| 0x56059538fde4 | /code/root/v/v(compiler__Parser_parse+0xe69)
| 0x5605953d0089 | /code/root/v/v(compiler__V_parse+0x15b)
| 0x5605953d0306 | /code/root/v/v(compiler__V_compile+0x261)
| 0x5605953f4eb6 | /code/root/v/v(main__main+0x649)
| 0x5605953f788b | /code/root/v/v(main+0x68)
| 0x7fee2618cb97 | /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)
| 0x56059535ceea | /code/root/v/v(_start+0x2a)