640 views|7 replies

2865

Posts

4

Resources
The OP
 

"Rust Practice" Rust language type overview [Copy link]

The new and special syntax of Rust language is hard to adapt to. Without a comprehensive understanding of Rust syntax, even a practical small program cannot be written. For example, if you compare the size of two values 0.1 and 1, an error will be reported, just like Elder Tang's "disasters at every step and suffering everywhere" when he was seeking scriptures. Why did Rust design such strict syntax rules? In fact, these language syntax features are designed for program problems that often appear or are neglected in C and C++ languages. The essence of Rust's syntax features is to track variables during compilation, with the purpose of using and reducing dynamic memory allocation and management tasks. This allows you to find program problems during the compilation stage, especially in memory management. Therefore, it is important to understand the restrictions of syntax when learning, and not to blindly use past programming syntax ideas.

The types of Rust are the same as other syntaxes, and are divided into simple variable types/scalar types (more appropriately called data types) and compound data types and combined data types (array types, slice types). Usually simple types are built-in types defined by the language syntax, and compound types are "user-defined types". Rust's simple data types are the same as other languages, such as: unsigned integers, integers, floating-point numbers, etc., but the types in Rust are very strict when comparing values. Different data types cannot be compared. If necessary, type conversion is required. Only data types with the same "trait" can use symbolic programming. When introducing types from other languages, you need to use macros such as: #! [allow(unused_variables)] to notify the compiler to relax compilation restrictions.

Tuples, Rust supports tuples. An interesting data type, "tuple" is a composite data type that can access variables using indexes, as shown in the following program:

fn main() {
   let tuple:(i32,f64,u8) = (-325,4.9,22);
   println!("{:?}",tuple);
}

The composite data type is composed of three simple types, access methods:

tuple.0  // -325
tuple.1  // 4.9
tuple.2  // 22

Array, this type is the most commonly used type in computers, but it is different from other languages. Rust's array has three types, immutable data list type, slice data type, and mutable array type VEC. The reason why Rust's array types are very detailed is to track the scope of variables when static, so careful planning is required.

Enumeration , which is similar to C/C++ language, but provides some programming convenience, such as match type.

Structure is the most complex type, and there are many actual concepts. However, the purpose of rust is that things that can be solved by static programming will not be delegated to the dynamic stage (runtime), and things that can be put into the stack memory will not be put into the heap memory. There is no object operation like C++ new in rust. Rust's new is actually an ordinary function, not a keyword function (constructor) like in C++, because C++ will allocate the memory of the object Object to the heap, which leads to complex memory management in C++, and problems such as dangling pointers and null pointers.

In addition, Rust does not have the concept of classes and structures. The structures in Rust are only used as index markers for associated functions. Although the calling rules look the same as those in C++, there is no "relationship" between the function and the structure.

shap.width,shap.height

It doesn't matter, it's just an independent function, unlike object-oriented languages like C++, where methods/functions are pointers or indexes belonging to objects. Rust just makes struct-related functions look like C++ syntax sugar.

Then there is the concept of trait, which is also a concept similar to interface attached to the structure, but without specific instantiation, it is a type conversion-related feature attached to the structure struct. This is a bit abstract, and the purpose of this design is to design a programming concept similar to "inheritance" in C++ syntax. Another way of saying it is that the conversion rules used for different struct types in Rust are not very strict, but they are similar.

The various types and strict type checking in Rust are the characteristics of Rust, and are also the guarantee that Rust can write secure software. Therefore, when learning, you must not associate it with other language concepts. Just know the purpose of Rust syntax.

This post is from Programming Basics

Latest reply

Recommend some rust introductory tutorials  Details Published on 2024-4-30 09:08
 

7422

Posts

2

Resources
2
 

Thanks for sharing, looking forward to the follow-up!

This post is from Programming Basics
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

6821

Posts

11

Resources
3
 
I haven't had time to study these days. Seeing you guys progress so fast, I can't keep up.
This post is from Programming Basics
 
 
 

1108

Posts

1

Resources
4
 

The main problem is the lack of simple and clear comparison instructions. Especially the experience of other programming languages cannot be reused.

This post is from Programming Basics

Comments

Just don't use the previous grammatical rules to understand Rust. If you want to understand it in depth, you can also think about why Rust does this. Combined with the content of Chapter 1, understand the purpose of Rust syntax, instead of looking for similar  Details Published on 2024-4-29 09:39
 
 
 

2865

Posts

4

Resources
5
 
hellokitty_bean posted on 2024-4-29 09:10 The main problem is the lack of simple and clear comparison instructions. Especially if you have experience in other programming languages, it cannot be reused

Just don't use the previous grammar rules to understand Rust. If you want to understand it in depth, you can also think about why Rust does this. Combined with the content of Chapter 1, understand the purpose of Rust syntax, instead of looking for similar

This post is from Programming Basics

Comments

Yes, yes. I think so too. I will unconsciously bring in old experiences and go crazy.  Details Published on 2024-4-29 15:38
 
 
 

1108

Posts

1

Resources
6
 
bigbat posted on 2024-4-29 09:39 Just don't use the previous grammatical rules to understand Rust. If you want to understand it in depth, you can also think about why Rust does this. Combined with the content of the first chapter...

Yes, yes. I think so too. I will unconsciously bring in old experiences and go crazy.

This post is from Programming Basics
 
 
 

3

Posts

0

Resources
7
 
Recommend some rust introductory tutorials
This post is from Programming Basics

Comments

If I were to recommend a book, I would recommend Rust in Practice. The advantage of this book is that it teaches you how to understand Rust syntax from a practical perspective. The disadvantage of this book is that it requires some programming experience. I personally think that it is difficult to learn Rust in the same way as other languages. Before reading this book, I studied  Details Published on 2024-4-30 09:24
 
 
 

2865

Posts

4

Resources
8
 
Long En 01 posted on 2024-4-30 09:08 Recommend some rust introductory tutorials

If I were to recommend something, I would recommend the book Rust Practice, which is currently available. The advantage of this book is that it teaches you to understand Rust syntax from a practical perspective, but the disadvantage is that you need to have some programming experience. I personally think it is difficult to get started with Rust in the same way as other languages. I have studied Rust before reading this book, but I didn't get the hang of it, even though I have been an old software engineer who has been engaged in MFC programming for many years.

This post is from Programming Basics
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list