当前位置:网站首页>Rust Basics
Rust Basics
2022-06-29 15:23:00 【User 1097444】
With Rust The fire of language , There was a surge in the front circle Rust wind —— Everything works Rust All the implementations are trying to use Rust rewrite , For example, the recent hot benchmarking Babel Of JavaScript/TypeScript compiler swc, I believe many people have tried .
For our front end , Such a hot language , Of course, we can't let it go , We must keep up with the trend of the times .
One 、 What is? Rust
Rust By Mozilla Leading the development of general 、 Compiled programming languages . The design criteria are “ Security 、 Concurrent 、 practical ”, Support functional 、 Hairstyle 、 Procedural and object-oriented programming style . —— Wikipedia
Two 、 Language features
1、 Language features
Rust It is similar to the language of metalanguage family in deep grammar Haskell Closer to the . Basically every part of a function body is an expression , Even control flow operators .
2、 Memory safety
Null pointers are not allowed in security code , Dangling pointer and data race . Values can only be initialized in a series of fixed forms , Require that all inputs have been initialized .
3、 memory management
Do not use an automatic garbage collection system , adopt RAII To manage memory and resources , Optionally, reference counts .
4、 ownership
All values have a unique owner , The valid range of the value is the same as that of the owner .
5、 Type polymorphism
Rust Our type system supports a mechanism similar to type classes , It's called “traits”, Be being Haskell Inspiring . This is a facility for specific homogenous laws , This is achieved by adding constraints to the type variable declaration . Other from Haskell Characteristics of , For example, higher type polymorphism is not supported , Details can be viewed :Rust Type polymorphism .
3、 ... and 、 data type
Rust There are several types in total : Integer type 、 floating-point 、 Boolean type 、 Character 、 The compound type .
1、 Integer type (i、u)
Rust The type of language is similar to C The language of the series , The shaping data is divided into signed and unsigned types according to whether it is signed or not .
digit | A signed | Unsigned |
|---|---|---|
8-bit | i8 | u8 |
16-bit | i16 | u16 |
32-bit | i32 | u32 |
64-bit | i64 | u64 |
128-bit | i128 | u128 |
... | ... | ... |
The default type of shaping is i32.(i、 u It means int、uint)
let a = 10; // i32
let b: i64 = 20; // i642、 floating-point (f)
Rust Support... Like other languages 32 Bit floating point (f32) and 64 Bit floating point (f64). By default , The floating point data type is 64 Bit floating point , Because modern computer processors can calculate two kinds of floating-point numbers at almost the same speed , but 64 Bit floating point numbers have higher precision .
let a = 10.0; // f64
let b: f32 = 20.0; // f323、 Boolean type (bool)
And js equally , The value is true or false.
4、 Character (char)
The character type size is 4 Bytes , representative Unicode Scalar values .
Be careful : Because there are two kinds of Chinese character coding (GBK and UTF-8), Therefore, the use of Chinese strings in programming may lead to garbled code , This is because the text encoding of the source program is inconsistent with that of the command line , So in Rust Both strings and characters must use UTF-8 code , Otherwise, the compiler will report an error .
let c = 'a';5、 The compound type
Arrays must be familiar to everyone , Is a set of data of the same type enclosed by brackets , But what we need to know is ,Rust The arrays in are fixed length , That is to say, we cannot add or delete an array after determining it .
let arr = [1, 2, 3, 4, 5];
let arr2: [i32; 5] = [1, 2, 3, 4, 5]; // The length is 5 Of i32 Array
let arr3 = [1; 3]; // be equal to [1, 1, 1]
let one = arr[0]; // 1Rust There is also a tuple type , It can contain different data types .
let tup: (i32, f32, char) = (10, 20.0, 'a');Four 、 Structure
The structure is a bit like us ts Medium interface
struct Person {
name: String,
sex: String,
age: u32
}
let p1 = Person {
name: String::from("abc"),
sex: String::from("male"),
age:18
}
// Structure update Syntax , similar js Deconstruction of
let p2 = Person{
name: String::from("123"),
..p1
}5、 ... and 、 Enumeration class
enum Phone {
IPhone, Huawei
}Enumeration classes are often associated with match Use with grammar , Used to implement the branch structure , Similar to... In other grammars switch, however Rust China does not support switch.
fn main() {
enum Phone {
IPhone(u32),
Huawei {url: String},
}
let phone = Phone::IPhone(123);
let phone2 = Phone::Huawei {url: String::from("hahaha")};
match phone {
Phone::IPhone(i) => {
println!("{}", i);
},
Phone::Huawei { url } => {
println!("{}", url);
}
}
match phone2 {
Phone::IPhone(i) => {
println!("{}", i);
},
Phone::Huawei { url } => {
println!("{}", url);
}
}
}
// 123
// hahahamatch In addition to being able to branch an enumerated class , You can also use integer 、 Floating point numbers 、 Character and string slice references (&str) Type of data .
You must handle exceptions when branching non enumerated classes , Underline _ Express .
6、 ... and 、 ownership
Ownership is Rust A syntax mechanism designed for efficient use of memory
The concept of ownership is to make Rust The concept of analyzing the usefulness of memory resources more effectively in the compilation phase to realize memory management .
// Move
// In this case, the first variable cannot be accessed
let s1 = String::from("hello");
let s2 = s1;
println!("{}, world!", s1); // error !s1 It's invalid
// clone
// This can
let s1 = String::from("hello");
let s2 = s1.clone();
println!("s1 = {}, s2 = {}", s1, s2);Keep up with the technological frontier , Dig deep into professional fields
Scan the code and pay attention to us !
边栏推荐
- SSL v** technical principle
- MySQL 数据库命名规范.PDF
- 第九章 APP项目测试(此章完结)
- 高分三号卫星(GF-3)简介
- message from server: “Host ‘xxxxxx‘ is blocked because of many connection errors; unblock with ‘m
- MCS: discrete random variable Pascal Distribution
- Secondary pointer
- 目前股票开户安全吗?可以直接网上开户吗
- ROS notes (10) - Launch file startup
- MCS: multivariate random variable polynomial distribution
猜你喜欢

MCS: discrete random variable - Hyper geometric distribution

CKS CKA ckad change terminal to remote desktop

File常用工具類, 流相關運用 (記錄)

Knowledge points: what are the know-how of PCB wiring?

For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?

信息学奥赛一本通2061:梯形面积

MySQL JSON array operation JSON_ array_ append、json_ array_ insert

Lumiprobe 活性染料丨环炔染料:AF488 DBCO,5 异构体

MCS:多元随机变量——离散随机变量

Construction and application of medical field Atlas of dingxiangyuan
随机推荐
西北工业大学遭境外电邮攻击
MCS: discrete random variable Poisson distribution
信息学奥赛一本通1002:输出第二个整数
知识点:PCB线路板布线都有哪些诀窍?
MCS:离散随机变量——Poisson分布
又拍云 Redis 的改进之路
Symfony framework security component firewall configuration
PyTorch 二维多通道卷积运算方式
BFD principle and configuration
Informatics Olympiad all in one 1002: output the second integer
雷达相关内容简介
Unity C basic review 28 - delegation with return (p449)
遥感典型任务分析
Leetcode notes: biweekly contest 81
Is it safe to open a stock account at present? Can I open an account online directly
华为软件测试笔试真题之变态逻辑推理题【二】华为爆火面试题
SOFARegistry 源码|数据同步模块解析
Unity C# 基础复习29——泛型委托(P451)
材质 动态自发光
MCS:离散随机变量——Hyper Geometric分布