您的位置:首页 > 文旅 > 美景 > 小米手机商城_无锡网站建设收费_网络营销专业就业公司_如何推广自己成为网红

小米手机商城_无锡网站建设收费_网络营销专业就业公司_如何推广自己成为网红

2025/1/11 4:51:12 来源:https://blog.csdn.net/m0_37719524/article/details/143699786  浏览:    关键词:小米手机商城_无锡网站建设收费_网络营销专业就业公司_如何推广自己成为网红
小米手机商城_无锡网站建设收费_网络营销专业就业公司_如何推广自己成为网红

Rust.Crate之claim包

  • 断言
    • Rust中的原生断言
    • claim Crate
  • 总结


断言

断言用于检查程序在运行期间特定条件是否为真的一种调试手段,多数出现在Debug模式下,断言一般不带有特别多的错误信息,只会把断言处发生的错误抛出到指定的位置。如下面的C++断言代码:
代码示例:


#include <cassert>
#include <iostream>int main() {int x = 10;assert(x > 0); // 断言x应该大于0,这个断言应该通过std::cout << "x is " << x << std::endl;int y = -1;assert(y > 0); // 断言y应该大于0,这个断言失败,程序将终止std::cout << "y is " << y << std::endl;return 0;
}

一般的,断言出现在Debug模式,或是单元测试中较多,一旦进入CICD阶段,出了错如果无法直接在输出中直接看出就需要看源码,这对于熟悉repo代码的开发者来讲并不是难事,但是对于新接手的容易出错的开发者来讲,并不是十分的友好,开发者会分一部分精力去适应原有的古早测试代码,那么随着业务量的扩大和框架的不断扩张,单元测试会越来越多,直观的断言也就越来越重要。

Rust中的原生断言

代码示例:

fn main() {let vector = vec![1, 2, 3, 4, 5];assert_eq!(vector.len(), 3);
}

终端输出内容:

assertion `left == right` failedleft: 5right: 3

从终端输出内容可见,没有任何携带的信息,对于问题也并不一目了然,尤其是在有业务场景的单元测试中出了问题就不可避免的要进入到具体的单元测试查看。

claim Crate

claim包最好用的便是可以讲错误信息输出到指定位置(终端或是log日志)。
代码示例:

fn main() {let result: Result<&str, &str> = Err("this is a test error");claim::assert_ok!(result);
}

终端输出:

assertion failed, expected Ok(..), got Err("this is a test error")

可见claim 具备将具体err展示出来的能力,除此之外claim crate还提供了一系列的宏,让Rust的测试更加的方便。

assert_err	Asserts that expression returns Err(E) variant.
assert_ge	Asserts that first expression is greater or equal than the second.
assert_gt	Asserts that first expression is greater than the second.
assert_le	Asserts that first expression is less or equal than the second.
assert_lt	Asserts that first expression is less than the second.
assert_matches	Asserts that expression matches any of the given variants.
assert_none	Asserts that expression returns None variant.
assert_ok	Asserts that expression returns Ok(T) variant.
assert_ok_eq	Asserts that expression returns Ok(T) variant and its value of T type equals to the right expression.
assert_pending	Asserts that expression returns Poll::Pending variant.
assert_ready	Asserts that expression returns Poll::Ready(T) variant.
assert_ready_eq	Asserts that left expression returns [Poll::Ready(T)] variant and its value of T type equals to the right expression.
assert_ready_err	Asserts that expression returns Poll::Ready(Err(E)) variant.
assert_ready_ok	Asserts that expression returns Poll::Ready(Ok(T)) variant.
assert_some	Asserts that expression returns Some(T) variant.
assert_some_eq	Asserts that left expression returns Some(T) variant and its value of T type equals to the right expression.
debug_assert_err	Asserts that expression returns Err(E) variant in runtime.
debug_assert_ge	Asserts that first expression is greater or equal than the second in runtime.
debug_assert_gt	Asserts that first expression is greater than the second in runtime.
debug_assert_le	Asserts that first expression is less or equal than the second in runtime.
debug_assert_lt	Asserts that first expression is less than the second in runtime.
debug_assert_matches	Asserts that expression matches any of the given variants.
debug_assert_none	Asserts that expression returns None variant in runtime.
debug_assert_ok	Asserts that expression returns Ok(T) variant in runtime.
debug_assert_ok_eq	Asserts that expression returns Ok(T) variant in runtime.
debug_assert_pending	Asserts that expression returns Poll::Pending variant in runtime.
debug_assert_ready	Asserts that expression returns Poll::Ready(T) variant in runtime.
debug_assert_ready_err	Asserts that expression returns Poll::Ready(Err(E)) variant in runtime.
debug_assert_ready_ok	Asserts that expression returns Poll::Ready(Ok(T)) variant in runtime.
debug_assert_ready_ok_eq	Asserts that left expression returns [Poll::Ready(T)] variant and its value of T type equals to the right expression in runtime.
debug_assert_some	Asserts that expression returns Some(T) variant in runtime.
debug_assert_some_eq	Asserts that expression returns Some(T) variant in runtime.

总结

后续会针对Rust单元测试方式进行记录和讲解。

“我们都无法确定自己的选择是否正确,只是一步步将选择变得正确”

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com