Rust Chinese Translation 23

Source: Internet
Author: User

4.9 Borrow and Asref


Borrow and asref characteristics are similar, but not the same. Here's a quick review of both.


4.9.1 Borrow
The borrow feature is when you write a data structure, and then you want to use it as a synonym object for its owner or borrower type. For example, HashMap has a GET method that uses borrow:
fn get<q:? Sized> (&self, K: &q), option<&v> where k:borrow<q>, Q:hash + Eq
This signature is very complex. The k parameter is the point of interest to us. It represents a HashMap parameter:
struct hashmap<k, V, S = randomstate> {
The k parameter is the key used by HashMap. So, looking at the signature of get (), we can try the get () function when key implements Borrow<q>. That way, we can generate a hashmap that uses string as key. But when we search, we use &AMP;STR.
Use Std::collections::hashmap;
Let Mut map = Hashmap::new (); Map.insert ("Foo". to_string (); assert_eq! (Map.get ("Foo"), Some (&42));
This is because the standard library implements the Impl Borrow<str&gt for string types.
For most types, when you need an owning or borrowing type, a &t is sufficient. And borrow is efficient when there are multiple borrowed values. A slice is one of those scenarios: you can get a &[t] or &mut [T]. If we want to get both of these types:
Use Std::borrow::borrow;
Use std::fmt::D isplay;
fn foo<t:borrow<i32> + display> (a:t) {
println! ("A is borrowed: {}", a);
}
Let mut i = 5;
Foo (&i);
Foo (&mut i);
The output is two: A is Borrowed:5
4.9.2 Asref
Asref is a custom trait. It is used to convert values into references in generic code. Like this:
Let S = "Hello". to_string ();
fn foo<t:adref<str>> (s:t) {Let slice = S.as_rf ();}
4.9.3 should I use that? We can see that they are alike: they can all handle all or borrowed types. But a little different.
When you need to abstract different types of borrowing, use borrow, or you want to create a data structure that handles all or borrowed values in the same way, such as hashing and comparisons.
When you need to convert a value directly to a reference, use Asref, and you are writing generic code.
4.10 Release Channels
The Rust Project uses a concept called a "distribution channel" to manage its release. It's important to understand when you choose which version of Rust to use.
4.10.1 Overview
There are three types of distribution channels: nightlybetastable
Nightly release boards are generated every day. Every 6 weeks, the last nightly release board is upgraded to Beta. At that time, it will only receive patches to resolve serious problems. After 6 weeks, the beta version is upgraded to stable and then to the next 1.x version.
This process is parallel. So every 6 weeks, on the same day, there will be nightly,beta,stable released. When 1.x is released, at the same time, 1. (x+1)-beta released, nightly will also become 1. (x+2)-nightly.
4.10.2 Select a version
In general, unless you are a special reason, you should choose the stable version. This version applies to a wide audience.
However, you can choose to use the nightly version, depending on how interested you are with rust. The cost is this: in the nightly version, you can use the unstable, new rust feature. However, the unstable features change, So any new nightly version could be your code crash. If you use the stable version, you cannot experience experimental features, but the next stable release will not have a serious impact on your code.
4.10.3 using CI to help build the rust ecosystem
What about the beta version? We encourage all rust programmers to use the stable version and test the beta version as well. Use the beta release in their ongoing system environment. This will help our team to provide early warning to avoid unexpected setbacks.
In addition, the test nightly version can capture backwards faster, and if you don't mind compiling 3 times, you can test all the versions.

Rust Chinese Translation 23

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.