google/comprehensive-rust

syntax: explain that rust pervasively allows separators to be used as terminators

开放

#2,071 创建于 2024年5月9日

 (0 条评论) (1 个反应) (1 位负责人)Rust (2,009 个派生)batch import
good first issue

仓库指标

星标
 (32,954 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

I don't think we have it anywhere in our speaker notes, but we should make an explicit mention of it. Usually when teaching I try to bring this up around when semicolons are discussed or when we talk about tuple and array types.

This is a nice feature of the language that's easy to miss if not called out: to minimize git diffs, almost every syntactic construct in Rust allows separators to also be used as terminators. This includes commas in tuple and array values, semicolons separating expressions (though the trailing semicolon does have semantic meaning in this case), and even pipes as leading delimiters in patterns (as in Standard ML or OCaml):

match 'x' {
	| 'a' => 1,
	| 'b'
	| 'c' => 2,
	| _ => 3,
}

贡献者指南