注释 Comments
Rust中注释和C语言及JavaScript一样 ,以 // 开头;多行注释在每一行前加上 // 。
VSCode中, Ctrl + / 快捷键是注释快捷键,可以迅速给代码添加注释格式。
在Rust中单行注释是 //COMMENTS ,多行注释是/*COMMENTS*/;
在JavaScript中,单行注释是 //COMMENTS ,多行注释是/*COMMENTS*/;
在HTML中是 <!--COMMENTS--> ;
在CSS中是 /*COMMENTS*/ ;
在Python中单行注释是 #COMMENTS ;多行注释是 '''COMMENTS''' (三个单引号或者三个双引号前后包裹)
在Powershell中 单行注释是 #COMMENTS ,和python一样;
在Powershell中 多行注释是 “<#”和 “#>”来引起一段注释,类似于HTML。
<#文件:test.ps1用途:脚本,执行test命令创建:--年--月--日作者:YY#>
个人感觉,除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】。
举例:
//! A doc comment that applies to the implicit anonymous module of this cratepub mod outer_module {//! - Inner line doc//!! - Still an inner line doc (but with a bang at the beginning)/*! - Inner block doc *//*!! - Still an inner block doc (but with a bang at the beginning) */// - Only a comment/// - Outer line doc (exactly 3 slashes)//// - Only a comment/* - Only a comment *//** - Outer block doc (exactly) 2 asterisks *//*** - Only a comment */pub mod inner_module {}pub mod nested_comments {/* In Rust /* we can /* nest comments */ */ */// All three types of block comments can contain or be nested inside// any other type:/* /* */ /** */ /*! */ *//*! /* */ /** */ /*! */ *//** /* */ /** */ /*! */ */pub mod dummy_item {}}pub mod degenerate_cases {// empty inner line doc//!// empty inner block doc/*!*/// empty line comment//// empty outer line doc///// empty block comment/**/pub mod dummy_item {}// empty 2-asterisk block isn't a doc block, it is a block comment/***/}/* The next one isn't allowed because outer doc commentsrequire an item that will receive the doc *//// Where is my item?}
命名规则
通常,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_case 或 SCREAMING_SNAKE_CASE 中,“单词”永远不应该只包含一个字母, 除非是最后一个“单词”。所以,我们有btree_map 而不是 b_tree_map,PI_2 而不是 PI2。
语句 Statement 和表达式 Expression
参考:The Rust Reference
Rust基本上是一种表达式语言,Rust中Expression比较重要。
语句以分号 ; 结尾,main() 函数体内部每一行分号结尾的代码就是一个语句。语句是块的组成部分,而块又是外部表达式或函数的组成部分。每段代码块都是有值的。代码块的值是代码块内最后一条语句的值。
表达式 Expression 总会产生一个值,所以表达式必然有类型。
表达式可在另一个表达式内嵌套,组合为一个新的表达式。
表达式加上分号 ; 就是一个语句。
语句 Statement
Rust有两种语句:声明语句 declaration statements 和表达式语句 expression statements
声明语句 Declaration statements

声明语句,顾名思义,用于声明
extern //
包括变量、静态变量、常量、结构体、函数等,以及通过 extern 和use关键字引入包和模块。
声明语句是将一个或多个名称引入封闭语句块的语句。声明的名称可能表示新变量或新项目。两种声明语句是item 声明和 let 语句。
Item declarations
item 声明语句与模块内的 item 声明的句法相同。声明一个 item在一个语句块内,就限定了这个item的范围是其所在的语句块。
该 item 【项目】没有给出规范路径,也没有可声明的任何sub-items【子项目】。由实现定义的关联项目仍然可以在外部范围内访问。如果合适,特征也是可以访问的。在其他方面与在模块内声明项目的含义相同。
没有隐式捕获包含函数的泛型参数、参数和局部变量。例如,inner 可能无法访问outer_var。
表达式 Expression

字面量表达式:由文字形式【Characters and strings】之一组成。它直接描述一个数字、字符、字符串或布尔值。
"hello"; // string type'5'; // character type5; // 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 }
fn main(){let a = 1;let x = if a == 1{20} else {40};let y = if a != 1{5} else {10};println!("the value of x is {}",x);println!("the value of y is {}",y);}// the value of x is 20// the value of y is 10
格式化 format!
模块 std::fmt:用于格式化和输出的模块。
用法:format! 宏与python中的 str.format 相同。
fn main(){let s1 = format!("s1 is a string.");println!("{}",s1);let s2 = format!("of course, {} is a string too.","s2");println!("{}",s2);let tup1 = format!("contains a tup: {:?}",(3,4,5));println!("tup1 {} ,it is a real tuple.",tup1);}// s1 is a string.// of course, s2 is a string too.// tup1 contains a tup: (3, 4, 5) ,it is a real tuple.
位置参数
fn main(){let x = 5;let y = 6;let z = 7;println!("x is {},y is {},z is {};\nz is {2},x is {0},y is {1}.",x,y,z);}//x is 5,y is 6,z is 7;//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版本。
