Insights: I feel like rust is like a seam monster. It has the shadow of python, java and cpp. Maybe this is the trend of new programming languages. Moreover, its various specifications are very strict and standardized, and it is more engineering than Java. If the specifications are not correct, there will be warnings.
Naming convention: snake-shaped naming method, the same as the Python writing method of deep learning.
Function and variable names in Rust code use the snake case specification style. In snake case, all letters are lowercase and underscores are used to separate words.
cargo
Basically, rust builds a project to facilitate management.
Create project:
cargo new pro_name
Compile:
cd pro_name
cargo build
run directly
cd pro_name
cargo run
Delete project
del pro_name -Force
Variables are immutable
Variables in rust are immutable by default. If you want to change them, use the mut keyword to modify the variables.
let foo = 3; // 不可变
let mut foo2 = 3; // 可以改变
& references are also immutable by default
crate
Translated into a container, the type is a library in cpp and a package in python.
use std::io; // 导入io包
use std::rand::Rng; // 导入随机数包
shadow
Rust allows hiding the value of an old variable using a new variable with the same name.
let a = 3; // 可以
let a = a * 3; // 可以
let a = a + 3; // 可以
boolean expression
In cpp, numbers can be implicitly converted to boolean values; in rust, numbers cannot be implicitly converted to boolean values.
cpp:
if(1) puts("YES");
else puts("NO");
rust:
if 1 == 1 {
// if 1 { } 错误
println!("YES");
} else {
println!("NO");
}
match
andexpect
expect
:
The Rust standard library has a number of Result
types called : a generic Result
and specialized versions in submodules, io::Result
e.g.
Result
Types are enumerations , often written as enums . An enumeration type holds a fixed set of values, called variants of the enumeration . Chapter 6 covers enumerations in more detail.
Result
The members are Ok
and Err
, Ok
the member indicates the success of the operation, and contains the value generated when the operation is successful. Err
Members mean that the operation failed and include the causes and consequences of the failure.
The purpose of these Result
types is to encode error handling information. Result
Values of a type, like other types, have methods defined on them. io::Result
Instances of have expect
a method . If io::Result
the value of the instance is Err
, expect
it will cause the program to crash and display expect
the information passed as the parameter. If read_line
the method returns Err
, it may be the result of an underlying operating system error. If io::Result
the value of the instance is Ok
, the value in expect
is obtained and returned unchanged. Ok
In this case, the value is the number of bytes entered by the user on standard input.
match
:
To match:
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
**Statements and Expressions**
The statement has no return value .
let x = 5; // 这是语句,没有返回值
let x = (let y = 5); // 错误, `y = 5` 没有返回值
A function call is an expression. A macro call is an expression. The curly brace (code block) we use to create a new scope, , {}
is also an expression.
let x = 5;
let y = {
let x = 3;
x + 1
};
// x = 5, y = 4
This expression:
{
let x = 3;
x + 1
}
is a code block whose value is 4
. This value let
is bound to as part of the statement y
. Notice the line without a semicolon at the end x+1
, unlike most lines of code you've seen. There is no semicolon at the end of the expression. If you add a semicolon to the end of an expression, it becomes a statement, and statements do not return a value. Keep this in mind as you explore functions and expressions that return values next.
function
Rust doesn't care where the function is defined, as long as it is defined (unlike the cpp system).
A function that returns a value
A function can return a value to the code that called it. We do not name the return value, but ->
declare its type after the arrow ( ). In Rust, the return value of a function is equal to the value of the last expression in the function body. You can return early from a function using return
the keyword and a specified value; however, most functions implicitly return the last expression.
fn main() {
println!("{}",foo());
}
fn foo() -> i32 {
let x = 3;
x + 1 // 表达式作为返回值
}
fn main() {
println!("{}",foo());
}
fn foo() -> i32 {
let x = 3;
return x + 1; // return 返回值
}
Function parameters
Functions can also be defined as having parameters , which are special variables that are part of the function signature . When a function has parameters (formal parameters), it can provide specific values (actual parameters) for those parameters. Technically, these specific values are called parameters ( arguments ), but in daily communication, people tend not to distinguish between using parameter and argument to represent variables in a function definition or specific values passed in when calling a function.
In a function signature, the type of each parameter must be declared. This was a carefully considered decision in Rust's design: requiring type annotations in function definitions means that the compiler doesn't require you to annotate types elsewhere in your code to indicate your intent.
fn main() {
println!("{}",foo(4));
}
fn foo(x:i32) -> i32 {
let x = x * x;
x + 1
}