1. Reference materials
2. Function function
Input: Verification code length length
Output: length length string consisting of letters and numbers
3. Implement the code
use rand::Rng;
use rand::rngs::ThreadRng;
fn generate_verify_code(length: u8) -> String {
// 使用动态数组保存验证码结果
let mut verify_code_vec: Vec<char> = Vec::new();
// 验证码组成库
const CHAR_ARRAY: [char; 62] = [
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'
];
// 随机生成 N 位验证码
let mut rng: ThreadRng = rand::thread_rng();
for _i in 0..length {
verify_code_vec.push(CHAR_ARRAY[rng.gen_range(0..CHAR_ARRAY.len())]);
}
// 返回生成的验证码
String::from_iter(verify_code_vec.iter())
}
fn main() {
println!("generate_verify_code [ {:?} ]", generate_verify_code(4))
}
4. Operation results
5. Grammar analysis
1. Function declaration
Image from Rust Course
a. Parameters and return values
fn generate_verify_code(length: u8) -> String {...}
- fn is the declaration keyword
- generate_verify_code is the function name
- length: u8 specifies parameter name and parameter type
- -> String specifies the return value type
The return value can be returned early using the return keyword, or using the last expression as the return.
// 特别注意,以 ";" 结尾的是语句,没有 ";" 结尾的才是表达式
// 1、表达式返回
String::from_iter(verify_code_vec.iter())
// 2、return 返回
return String::from_iter(verify_code_vec.iter())
b. No return value()
The main function main is a function with no parameters and no return value . No return value is also a value. In fact, it will implicitly return a unit type () .
fn main() {
// 等同于
fn main() -> () {
c. Never return
When used as a function return type, it means that the function will never return (diverge function). In particular, this syntax is often used as a function that will cause the program to crash.
!
——Rust Course
// 永不返回的发散函数,这种语法往往用做会导致程序崩溃的函数
fn diverge_function() -> ! {
// panic!("ERROR!!!");
loop {
println!("ERROR")
};
}
fn main() {
println!("generate_verify_code [ {:?} ]", generate_verify_code(4));
// 永不返回的发散函数,程序止步于此,无法继续往下走
diverge_function();
// Unreachable code
println!("generate_verify_code [ {:?} ]", generate_verify_code(4));
}
As you can see, there is a compile-time warning that the statements below the divergent function cannot be executed.
2. Variables and constants
// 由 let 关键字定义变量,但是变量是不可变的,需要 mut 关键字修饰才能改变它的值
// 这里定义了一个 char 类型的动态数组,数组的值可变
let mut verify_code_vec: Vec<char>
// 由 const 关键字定义常量,这里定义了一个 char 类型长度为 62 的数组
const CHAR_ARRAY: [char; 62]
But how come the i variable in the for loop has a _ prefix?
for _i in 0..length {
Generally, variables must be used after they are defined, and unused variables can be written with an underscore _ to tell the compiler to ignore the variable, otherwise the compiler will prompt an alarm. Both of the following writing methods are acceptable.
// 1、忽略变量 i
for _i in 0..length {
// 2、用 _ 忽略循环中的值
for _ in 0..length {
3. Dynamic arrays and static arrays
//TODO