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

Example Analysis of variables in Rust

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

Share

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

This article shares with you the content of a sample analysis of variables in Rust. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

01 variables and bindings

Variable specifies the name of a storage unit (Memory Location) that stores a specific type of value.

Rust is a statically typed language and cannot change variable types at run time.

Unlike most programming languages you are familiar with, variables in Rust are generally called bindings instead of declarations (borrowed from functional languages, using the keyword let binding). What's the difference?

There are generally several ways to declare variables in the Go language:

Var age = 10 var age int var age int = 10 age: = 10 / / can only be used inside the function / / you can group var (age = 10 birthday = "01-01")

Variables are declared in Go, either without specifying the type (type derivation is done) or without giving the initial value (there will be a default initial value).

In Rust, there are relatively few forms, and there are mainly two kinds:

Let age = 10; let age: i32 = 10

Like Go, Rust can derive types most of the time. When displaying the specified type, you need to add:. As for the type, I will explain it later.

So why is variable creation in Rust generally called binding?

1) Rust, like C, variables must be initialized before they can be used (unused variables will be warned). The following code compiles an error:

Fn main () {let age: i32; println! ("age is {}", age);} / / error [E0381]: borrow of possibly-uninitialized variable: `age`

2) in Rust, an association is established between identifiers (such as variable age) and values (such as 10) through the let keyword. Indicate the ownership relationship. This means that this memory now belongs to age.

Friends who are familiar with JS should be very kind to var and let, but the difference between them is not the same as the difference between var in Go and let in Rust.

02 variability

You must be particularly surprised to see the following code report an error for the first time:

Fn main () {let age = 10; println! ("age is {}", age); age = 11; println! ("age is {}", age);} / / error [E0384]: cannot assign twice to immutable variable `age`

Yes, variables in Rust are immutable by default (well, variables are immutable. But it's not a constant. This is also an important feature of memory management in Rust.

What if I want variables to be variable? Rust provides the keyword mut, which is called variable binding:

Fn main () {let mut age = 10; println! ("age is {}", age); age = 11; println! ("age is {}", age);}

In general, we should give priority to creating immutable variables and use mutable variables only when we really need them.

03 hide (shadow)

Because variables are immutable by default, there is also such a "weird" situation in Rust. The following code is fine:

Fn main () {let age = 10; println! ("age is {}", age); let age = 11; println! ("age is {}", age);}

In Go, the affirmative statement is repeated.

This "repetition" syntax for creating variables with the same name is called Shadow in Rust. In other words, what was created last time is hidden by what is created this time. What's the use, exactly?

For example, code like this is quite common in Go:

AgeStr: = req.FormValue ("age") age, err: = strconv.Atoi (ageStr)

That is, the same value, because of different types, needs to be represented by two variables with different names. But in Rust, you can do this:

Fn main () {let age = "10"; let age = age.parse:: (). Unwrap (); println! ("age is {}", age);}

However, this grammar has both advantages and disadvantages. When it comes to scope, pay special attention to hidden problems. This is very similar to the "pit" of the short statement (: =) in Go. For code like this, the final age is still 10: (the actual code is usually not so obvious)

Fn main () {let age = 10; {let age = "abc"; println! ("age is {}", age);} println! ("age is {}", age);} / / age is abc / / age is 10

It can be seen that hiding only takes effect within its scope.

Thank you for reading! This is the end of this article on "sample Analysis of variables in Rust". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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