注释 Comments

Rust中注释和C语言及JavaScript一样 ,以 // 开头;多行注释在每一行前加上 //

VSCode中, Ctrl + / 快捷键是注释快捷键,可以迅速给代码添加注释格式。

Rust中单行注释是 //COMMENTS ,多行注释是/*COMMENTS*/

在JavaScript中,单行注释是 //COMMENTS ,多行注释是/*COMMENTS*/

在HTML中是 <!--COMMENTS--> ;
在CSS中是 /*COMMENTS*/ ;

在Python中单行注释是 #COMMENTS ;多行注释是 '''COMMENTS''' (三个单引号或者三个双引号前后包裹)

在Powershell中 单行注释是 #COMMENTS ,和python一样;
在Powershell中 多行注释是 “<#”和 “#>”来引起一段注释,类似于HTML。

  1. <#
  2. 文件:test.ps1
  3. 用途:脚本,执行test命令
  4. 创建:--年--月--日
  5. 作者:YY
  6. #>

个人感觉,除HTML外,有编辑器的辅助下,还是 // 仅单行注释最好。

文档注释

doc-comments 详见 The Rust Reference
内部文档注释:
doc注释以三个斜杠开始(///),相当于 #[doc=”…”]。比如,/// Foo 相当于 #[doc=”Foo”]。
block doc注释(/…*/),相当于 #[doc=”…”]。比如, / Bar / 相当于 #[doc=”Bar”]。
/// … 相当于 #[doc=”…”]
/
/ 相当于 #[doc=”…”]
parent of the comment :
以 //! 开头的行注释 及 /!…/ 块注释 是适用于该注释的父条目的文档注释,而非子条目。
//! … 相当于 #![doc=”…”]
/!…/ 相当于 #![doc=”…”]**
也就是说,它们相当于写#![doc=”…”]。
//! 注释通常用于记录占用源文件的模块。
文档注释中不允许单独的 \r 【后面没有跟\n】。

举例:

  1. //! A doc comment that applies to the implicit anonymous module of this crate
  2. pub mod outer_module {
  3. //! - Inner line doc
  4. //!! - Still an inner line doc (but with a bang at the beginning)
  5. /*! - Inner block doc */
  6. /*!! - Still an inner block doc (but with a bang at the beginning) */
  7. // - Only a comment
  8. /// - Outer line doc (exactly 3 slashes)
  9. //// - Only a comment
  10. /* - Only a comment */
  11. /** - Outer block doc (exactly) 2 asterisks */
  12. /*** - Only a comment */
  13. pub mod inner_module {}
  14. pub mod nested_comments {
  15. /* In Rust /* we can /* nest comments */ */ */
  16. // All three types of block comments can contain or be nested inside
  17. // any other type:
  18. /* /* */ /** */ /*! */ */
  19. /*! /* */ /** */ /*! */ */
  20. /** /* */ /** */ /*! */ */
  21. pub mod dummy_item {}
  22. }
  23. pub mod degenerate_cases {
  24. // empty inner line doc
  25. //!
  26. // empty inner block doc
  27. /*!*/
  28. // empty line comment
  29. //
  30. // empty outer line doc
  31. ///
  32. // empty block comment
  33. /**/
  34. pub mod dummy_item {}
  35. // empty 2-asterisk block isn't a doc block, it is a block comment
  36. /***/
  37. }
  38. /* The next one isn't allowed because outer doc comments
  39. require an item that will receive the doc */
  40. /// Where is my item?
  41. }

命名规则

通常,Rust 倾向于为“类型级”结构(类型和 traits)使用 CamelCase 而为“值级”结构使用 snake_case 。

条目 约定
Crates snake_case
(但倾向于单个词)
Modules snake_case
Types CamelCase
Traits CamelCase
Enum variants CamelCase
Functions snake_case
Methods snake_case
General constructors new
with_more_details
Conversion constructors from_some_other_type
Local variables snake_case
Static variables SCREAMING_SNAKE_CASE
Constant variables SCREAMING_SNAKE_CASE
Type parameters 简洁 CamelCase
,通常单个大写字母:T
Lifetimes 短的小写: 'a

CamelCase中, 首字母缩略词被当成一个单词:用 Uuid 而不是 UUID
snake_case 中,首字母缩略词全部是小写: is_xid_start
snake_caseSCREAMING_SNAKE_CASE 中,“单词”永远不应该只包含一个字母, 除非是最后一个“单词”。所以,我们有btree_map 而不是 b_tree_mapPI_2 而不是 PI2

语句 Statement 和表达式 Expression

参考:The Rust Reference
Rust基本上是一种表达式语言,Rust中Expression比较重要。

语句以分号 ; 结尾,main() 函数体内部每一行分号结尾的代码就是一个语句。语句是块的组成部分,而块又是外部表达式或函数的组成部分。每段代码块都是有值的。代码块的值是代码块内最后一条语句的值。

表达式 Expression 总会产生一个值,所以表达式必然有类型。
表达式可在另一个表达式内嵌套,组合为一个新的表达式。
表达式加上分号 ; 就是一个语句。

语句 Statement

Rust有两种语句:声明语句 declaration statements 和表达式语句 expression statements

声明语句 Declaration statements

03 其它语法注解★★--self--★★ - 图1
声明语句,顾名思义,用于声明

  1. extern //

包括变量、静态变量、常量、结构体、函数等,以及通过 extern use关键字引入包和模块。

声明语句是将一个或多个名称引入封闭语句块的语句。声明的名称可能表示新变量或新项目。两种声明语句是item 声明和 let 语句。

Item declarations

item 声明语句与模块内的 item 声明的句法相同。声明一个 item在一个语句块内,就限定了这个item的范围是其所在的语句块。
该 item 【项目】没有给出规范路径,也没有可声明的任何sub-items【子项目】。由实现定义的关联项目仍然可以在外部范围内访问。如果合适,特征也是可以访问的。在其他方面与在模块内声明项目的含义相同。
没有隐式捕获包含函数的泛型参数、参数和局部变量。例如,inner 可能无法访问outer_var。

表达式 Expression

03 其它语法注解★★--self--★★ - 图2

字面量表达式:由文字形式【Characters and strings】之一组成。它直接描述一个数字、字符、字符串或布尔值。

  1. "hello"; // string type
  2. '5'; // character type
  3. 5; // integer type

Path expressions【路径表达式?】:Paths【标识符为 : : 】用作表达式上下文的路径表示局部变量或item项。解析为局部或静态变量的路径表达式是 place expressions 位置表达式,其他路径是值表达式。


单目运算符表达式
双目运算符表达式

Rust表达式又分为“左值”lvalue 和“右值”rvalue。
lvalue:表达式可以表达一个内存地址,可以放到赋值运算符左边使用。
rvalue:非lvalue的均为rvalue。


在JavaScript中,三目运算符【?:】:expression ? sentence1 : sentence2
但是在Rust中没有三目运算符。可以直接用 if-else 简写:let variable = if expression1{ value1 } else { value2 }

  1. fn main(){
  2. let a = 1;
  3. let x = if a == 1{20} else {40};
  4. let y = if a != 1{5} else {10};
  5. println!("the value of x is {}",x);
  6. println!("the value of y is {}",y);
  7. }
  8. // the value of x is 20
  9. // the value of y is 10

格式化 format!

模块 std::fmt:用于格式化和输出的模块。
用法:format! 宏与python中的 str.format 相同。

  1. fn main(){
  2. let s1 = format!("s1 is a string.");
  3. println!("{}",s1);
  4. let s2 = format!("of course, {} is a string too.","s2");
  5. println!("{}",s2);
  6. let tup1 = format!("contains a tup: {:?}",(3,4,5));
  7. println!("tup1 {} ,it is a real tuple.",tup1);
  8. }
  9. // s1 is a string.
  10. // of course, s2 is a string too.
  11. // tup1 contains a tup: (3, 4, 5) ,it is a real tuple.

位置参数

  1. fn main(){
  2. let x = 5;
  3. let y = 6;
  4. let z = 7;
  5. println!("x is {},y is {},z is {};\nz is {2},x is {0},y is {1}.",x,y,z);
  6. }
  7. //x is 5,y is 6,z is 7;
  8. //z is 7,x is 5,y is 6.

大括号内没有位置参数时,默认从字符串后面的参数(实参)中,从0开始,按0,1,2的顺序提取;
有默认参数时,按参数指定提取字符串后面的参数(实参)。但是所有的参数必须要用上。
比如上例,若给定了x,y,z,t 四个参数,但是只用了x,y,z三个参数,编译时就会报错。

检查rust版本

检查版本:使用命令 rustc --version 在CMD中,或者VSCode的终端中可以查看已安装的rust版本。