Skip to content
📖 Auto 编程语言(主书)本书目录

附录 D:标准库索引 ​

本附录按字母顺序列出本书中演示的所有标准库函数和类型。详细的代码示例和跨语言 对比请参阅第 21 章(标准库导览)。

std::core ​

每个 Auto 程序中无需显式 use 即可使用的核心类型和宏。

名称签名类别说明章节
Bool--类型布尔类型,值为 true 或 false2
Float--类型64 位浮点数类型2
format!format!(模板, 参数...) String宏将值插入字符串模板进行格式化2, 21
Int--类型64 位有符号整数类型2
panic!panic!(消息)宏输出错误信息并中止程序9
printprint(值)函数将值写入标准输出,不追加换行符4, 21
printlnprintln(值)函数将值写入标准输出并追加换行符1, 2, 21
String--类型堆分配的 UTF-8 文本字符串2

std::string ​

String 类型上的文本处理方法。完整示例见清单 21-1。

名称签名类别说明章节
containss.contains(needle) bool方法若 needle 是 s 的子串则返回 true21
ends_withs.ends_with(suffix) bool方法若 s 以 suffix 结尾则返回 true21
joinlist.join(sep) String方法用 sep 连接列表中的元素21
lens.len() int方法返回 s 的字节长度4, 18
replaces.replace(old, new) String方法返回 s 的副本,将所有 old 替换为 new21
splits.split(delimiter) List<String>方法将 s 按分隔符拆分为子串列表4, 21
starts_withs.starts_with(prefix) bool方法若 s 以 prefix 开头则返回 true21
to_lowers.to_lower() String方法返回 s 的小写副本21
to_uppers.to_upper() String方法返回 s 的大写副本21
trims.trim() String方法去除首尾空白字符21

std::math ​

数学函数和常量。见清单 21-2。

名称签名类别说明章节
absabs(x) T函数返回 x 的绝对值21
ceilceil(x) Float函数向上取整21
clampclamp(value, lo, hi) T函数将 value 约束在 [lo, hi] 范围内21
floorfloor(x) Float函数向下取整21
maxmax(a, b) T函数返回 a 和 b 中的较大值21
minmin(a, b) T函数返回 a 和 b 中的较小值21
powpow(base, exp) Float函数计算 base 的 exp 次幂21
roundround(x) Float函数四舍五入到最近的整数21
sqrtsqrt(x) Float函数返回 x 的平方根21

std::collections ​

List<T>、Map<K,V> 和 Set<T> 上的操作。迭代器方法(.map()、.filter()、 .fold())在第 19 章中介绍;以下为额外的集合辅助方法。 见清单 21-4。

名称签名类别说明章节
chunklist.chunk(size) List<List<T>>方法将列表分割为最多 size 个元素的子列表21
containslist.contains(value) bool方法若 value 在列表中则返回 true4
filterlist.filter(pred) List<T>方法仅保留满足谓词 pred 的元素19
flattenlist.flatten() List<T>方法将列表的列表展平为单层列表21
foldlist.fold(init, fn) T方法用累加器将列表归约为单个值19
is_emptylist.is_empty() bool方法若集合为空则返回 true4
lenlist.len() int方法返回元素个数4
maplist.map(fn) List<U>方法对每个元素应用 fn 进行变换19
pushlist.push(value)方法在列表末尾追加 value4
reverselist.reverse() List<T>方法返回元素顺序反转的新列表21
sortlist.sort() List<T>方法返回排序后的新列表(默认升序)4, 21
uniquelist.unique() List<T>方法去重并保留插入顺序21
ziplist.zip(other) List<(T, U)>方法将两个列表合并为元组列表21

std::fs ​

文件 I/O、路径操作和目录操作。这些函数使用 !T 结果类型进行错误处理。 见清单 21-3。

名称签名类别说明章节
copycopy(src, dst)函数将文件从 src 复制到 dst21
existsexists(path) bool函数若 path 处的文件或目录存在则返回 true14, 21
list_dirlist_dir(path) List<String>函数列出 path 目录下的条目21
mkdirmkdir(path)函数创建目录(必要时同时创建父目录)21
path_joinpath_join(段...) String函数使用平台分隔符拼接路径14, 21
read_fileread_file(path) !String函数读取文件全部内容为字符串14, 21
removeremove(path)函数删除文件或空目录21
write_filewrite_file(path, content)函数将 content 写入文件,创建或覆盖21

std::json ​

JSON 序列化与反序列化。适用于任何实现了 Serialize 和 Deserialize 规范 的类型。见清单 21-6。

名称签名类别说明章节
from_json!from_json!(str, T) !T宏将 JSON 字符串反序列化为 T 类型的值21
to_jsonto_json(value) String函数将值序列化为紧凑的 JSON 字符串21
to_json_prettyto_json_pretty(value) String函数将值序列化为带缩进的 JSON 字符串21

std::time ​

用于处理日期、时间和时间段的类型与函数。见清单 21-5。

名称签名类别说明章节
Duration--类型一段时间跨度(如 2 小时 30 分钟)21
Duration.hoursDuration.hours(n) Duration函数创建 n 小时的时长21
Duration.minutesDuration.minutes(n) Duration函数创建 n 分钟的时长21
Duration.secondsDuration.seconds(n) Duration函数创建 n 秒的时长21
Time--类型时间点(时刻)21
Time.nowTime.now() Time函数返回当前时间21
Time.parseTime.parse(str, fmt) !Time函数按格式说明符解析时间字符串21
formattime.format(fmt) String方法按 strftime 格式说明符将时间格式化为字符串21
total_minutesdur.total_minutes() int方法将时长转换为总分钟数21
total_secondsdur.total_seconds() int方法将时长转换为总秒数21

std::thread ​

用于生成线程和控制执行的并发原语。见第 15 章和 第 16 章。

名称签名类别说明章节
joinhandle.join()方法等待生成的线程完成15
sleepsleep(duration)函数阻塞当前线程指定时长15
spawnspawn 函数关键字将函数作为新的 actor/线程启动执行15
yieldyield关键字将控制权交还给运行时调度器15

std::testing ​

断言和测试基础设施。见第 18 章。

名称签名类别说明章节
#\[test\]#[test]属性将函数标记为测试,由 automan test 运行18
assertassert(cond)函数若 cond 为 false 则 panic18
assert_eqassert_eq(a, b)函数若 a 不等于 b 则 panic;失败时打印两个值18
assert_neassert_ne(a, b)函数若 a 等于 b 则 panic;失败时打印两个值18
test_maintest_main()函数运行所有标记了 #[test] 的函数18

基于 MIT 许可发布。