Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic data types of Rust

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the basic data types of Rust". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the basic data types of Rust".

Rust, like Go, is a statically typed language, which means that the type of each variable must be explicit. Similar to Go, in most cases, the Rust compiler can infer the type of a value, without requiring us to display the specification, and write it a bit like a weakly typed language. In some cases, however, the compiler must be explicitly told what type we use, which is called "type annotation" (type annotations) in Rust.

For type annotations, take a common example:

Let guess = "42" .parse () .expect ("Not a number!")

This converts the string "42" into the number 42. In the Go language, you typically do this:

Guess, err: = strconv.Atoi ("42") if err! = nil {panic (err)}

But the above Rust code will report an error:

Error [E0282]: type annotations needed-- > src/main.rs:2:9 | 2 | let guess = "42a" .parse (). Expect ("Not a number!"); ^ consider giving `guess`a type

This is still not quite the same as Go. Many times in Go, the numeric type will be int.

To fix this problem, we need to specify a type for number, such as U32.

Let guess: U32 = "42" .parse () .expect ("Not a number!")

Complaint: in Rust, type annotations are placed after variables, as in Go. But variables and types in Rust have to be directly followed by a colon (:), and generally the colon is followed by the variable name (as suggested by rustfmt). I don't know what are the special needs of colons?!

Rust has the following basic data types built into it:

Integer type

Have matching integers: i8, i16, i32, i64, i128, isize

Unsigned integers: U8, U16, U32, U64, U128, usize

Floating point type: F32, F64

Boolean: bool

Character type: char

01 Integer Type

Organize the integer types into a table as follows: (compare with the corresponding types in Go language)

Length signed unsigned Go signed Go unsigned 8-biti8u8int8uint816-biti16u16int16uint1632-biti32u32int32uint3264-biti64u64int64uint64128-biti128u128--archisizeusizeintuint

Complaint: sometimes Rust is really economical. Int and uint are directly omitted from I and umai function are abbreviated to fn. But sometimes it is tedious (not concise), such as the colon between variables and types mentioned earlier.

Here in the form of u and I, it also takes some time to get used to it.

There are two points:

There are no 128bit integers in Go

Isize and usize correspond to int and uint in Go, and their length depends on the computer architecture in which the program is running: they are 64-bit on 64-bit architecture and 32-bit on 32-bit architecture

In Go, the default type of an integer variable is int, as evidenced by the following code:

X: = 32 fmt.Printf ("% T\ n", I) / / output: int

What is the default type in Rust?

I wanted to find a way in Rust to print variable types, and I found this on the Internet (a little frustrating):

/ / print functions of variable types. If you don't understand this function, leave it first. Fn print_type_of (_: & T) {println! ("{}", std::any::type_name:: ())} fn main () {let x = 32; print_type_of (& x); / / output: i32}

You can see that the default type of integer variable in Rust is i32 (even on 64-bit machines, it is i32). To some extent, it is suggested that int type is generally recommended for integers in Go, while i32 type is generally recommended in Rust. So why can't the initial parse be inferred to be i32 by default? Afraid of overflow?)

Smarter type inference

As mentioned above, Rust, like Go, supports type inference. But Rust's inference is more intelligent. How intelligent is it? Look at the following code:

/ / print variable type function fn print_type_of (_: & T) {println! ("{}", std::any::type_name:: ())} fn main () {let x = 32; let y: i8 = x; print_type_of (& x); print_type_of (& y)}

According to the above explanation, x should be the default type: i32. But in fact, both x and y are of type i8. That is, because the type of x is not specified (type annotation), the Rust compiler infers from the context (actually let y: i8 = x) that the type of x should be the same as y, that is, i8.

In Go, int8 and int are not implicitly converted, and neither is Rust, which must be converted to display. But Rust's intelligent type inference allows developers to write less code for type conversion.

For example, the above code does not work in the Go language:

Package main import ("fmt") func main () {x: = 32 var y int8 = x fmt.Printf ("% T\ n", x) fmt.Printf ("% T\ n", y)}

Will report an error:

Cannot use x (type int) as type int8 in assignment

That is, type inference in Go does not take context into account, so there is no Rust intelligence.

Because of the power of the compiler, there will be type prompts in VSCode (install rust-analyzer), so the above print_type_of function is not needed. Make a motion picture and notice the type change of x above:

In addition, the isize and usize types are generally used as indexes for certain collections, as you'll see in a later article.

I have listed the range of representations of various types, because this series is not for people with no programming experience. This series is more of a Rust tutorial for Go enthusiasts, so what is consistent with Go may not be covered.

02 floating point type

Like Go, Rust has two floating point types: F32 and f64, corresponding to float32 and float64 in Go. Like Go, the default type is f64, and you can specify a specific floating-point type through type annotations.

Let x = 2.0; / / default is f64

In general, integer types and floating-point types become numeric types.

Numeric types have something in common. For example, they all support basic mathematical operations. In addition, in addition to specifying a type through type annotations, a numeric type can also specify a type with a type suffix after the literal value, such as:

Let x = 2.0f32; / f32 type let y = 32i64; / / i64 type 03 Boolean type

Like the Go language, Boolean types in Rust are represented by bool (why not abbreviations such as b, bl, etc.)? Ha). There are two possible values: true and false.

Fn main () {let t = true; let f: bool = false; / / explicitly specify type comments} 04 character type

The char in Rust represents the character type, which is the basic type of Rust, and the literal value is specified by single quotation marks.

Let a = 'a'; let b =' medium'; let c ='?'

It can be seen that the char type in Rust is the same as rune in Go, which represents the Unicode code point, accounting for 4 bytes.

Because strings in Rust are complex and not a basic type, I'll explain them later.

Thank you for your reading, the above is the content of "what are the basic data types of Rust". After the study of this article, I believe you have a deeper understanding of what the basic data types of Rust are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report