本文簡要介紹rust語言中 Macro core::assert_matches::assert_matches
的用法。
用法
pub macro assert_matches {
($left : expr, $(|) ? $($pattern : pat_param) | + $(if $guard : expr) ? $(,)
?) => { ... },
($left : expr, $(|) ? $($pattern : pat_param) | + $(if $guard : expr) ?,
$($arg : tt) +) => { ... },
}
斷言一個表達式匹配任何給定的模式。
就像在match
表達式中一樣,該模式可以選擇性地後跟if
和一個可以訪問由該模式綁定的名稱的保護表達式。
Panics時,此宏將打印表達式的值及其調試表示。
像 assert!
一樣,這個宏有第二種形式,可以提供自定義的Panics消息。
例子
#![feature(assert_matches)]
use std::assert_matches::assert_matches;
let a = 1u32.checked_add(2);
let b = 1u32.checked_sub(2);
assert_matches!(a, Some(_));
assert_matches!(b, None);
let c = Ok("abc".to_string());
assert_matches!(c, Ok(x) | Err(x) if x.len() < 100);
相關用法
- Rust assert_ne用法及代碼示例
- Rust assert_eq用法及代碼示例
- Rust assert用法及代碼示例
- Rust array.map用法及代碼示例
- Rust addr_of_mut用法及代碼示例
- Rust alloc_zeroed用法及代碼示例
- Rust array.split_array_ref用法及代碼示例
- Rust array用法及代碼示例
- Rust array.zip用法及代碼示例
- Rust align_of用法及代碼示例
- Rust always_abort用法及代碼示例
- Rust array.split_array_mut用法及代碼示例
- Rust available_parallelism用法及代碼示例
- Rust addr_of用法及代碼示例
- Rust abort用法及代碼示例
- Rust align_of_val用法及代碼示例
- Rust array.each_mut用法及代碼示例
- Rust array.each_ref用法及代碼示例
- Rust args用法及代碼示例
- Rust alloc用法及代碼示例
- Rust args_os用法及代碼示例
- Rust align_of_val_raw用法及代碼示例
- Rust UdpSocket.set_multicast_loop_v6用法及代碼示例
- Rust i64.overflowing_add_unsigned用法及代碼示例
- Rust Box.downcast用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 Macro core::assert_matches::assert_matches。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。