Rust|常用函数的描述表格
格式:
名 + 文档链接 | 曰
| `[函数名](对应文档链接)` | 浓缩描述 |
| ------------------------ | -------- |
| `[函数名](对应文档链接)` | 浓缩描述 |
注意:表格的第一列,基本都是网址链接,只不过它的 css 格式是代码块。
目录
按功能/主题分类
基础
fn main |
main 函数是一个特殊的函数:在可执行的 Rust 程序中,它总是最先运行的代码 |
---|---|
unwrap() |
Ok /Some 就返回里面的值,Err /None 就 panic,让程序崩溃 |
try!() |
语法糖? , Ok /Some 就返回里面的值(比如:赋个值什么的), Err /None 就将错误值,返回到上层(函数) |
输出
println! |
在屏幕上打印文本,macro(宏) |
---|---|
write!(&mut w, "formatted {}", "arguments").unwrap(); |
assert_eq!(w, b"formatted arguments"); |
宏
! |
当看到函数名后,跟着符号 ! 的时候,就意味着调用的是宏,而不是普通函数。 |
---|---|
assert_eq!(a, b); |
断言a/b 相等,用的是PartialEq 。 |
条件编译
cfg()
对源代码进行条件区分,true
就有,false
剔除编译阶段cfg_attr()
对第一参数作为条件,那(后续参数)true
就有,false
没有
#[cfg(target_os = "macos")] |
只有目标操作系统是 macos,才为true |
---|---|
#[cfg_attr(feature = "magic", sparkles, crackles)] |
当feature = "magin" 为true ,sparkles 与 crackles 会启用 |
属性(Attribute)
属性,是 Rust 对各项标记/相关语法形式的统称,看语法:
- (内属性)InnerAttribute:
#![ Attr ]
:apply to the item that the attribute is declared within. (应用 Attr 所定义的项) - (外属性)OuterAttribute:
#[ Attr ]
:apply to the thing that follows the attribute. (应用 Attr ) - Rust 内置的属性
代码生成
#[inline] |
提示内联代码。 |
---|
测试
#[test] |
(函数)作为测试运行 |
---|---|
#[cfg(test)] |
备选:通过条件编译达到同样目的 |
- 注意: 测试模式是通过,
rustc --test
命令或是cargo test
启用的。
派生(derive)
#[derive(PartialEq, Clone)] |
为数据结构自动实现(impl ),PartialEq 和Clone trait |
---|---|
#[proc_macro_derive(AnswerFn) |
定义一个派生宏AnswerFn ,可以使用#[derive(AnswerFn)] |
标签:
Rust
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
2021-05-14 [计数dp] 整数划分(模板题+计数dp+完全背包变种题)
2020-05-14 imos-累积和法
2020-05-14 POJ 1860 - Currency Exchange
2020-05-14 POJ 3268 Silver Cow Party 题解 《挑战程序设计竞赛》