简体   繁体   中英

What does “alias” mean with respect to the `Sync` trait?

I'm learning Rust, trying to figure out the std::marker::Sync trait. The documentation for Sync starts with the following (version 1.1), emphasis mine:

Types that can be safely shared between threads when aliased .

The precise definition is: a type T is Sync if &T is thread-safe. In other words, there is no possibility of data races when passing &T references between threads.

As one would expect, primitive types like u8 and f64 are all Sync , and so are simple aggregate types containing them (like tuples, structs and enums).

This makes zero sense to me, starting with the first sentence. I thought aliasing has to do with statements such as:

type Name = String;

What does this have to do with synchronization? Perhaps the term “alias” is overloaded here, and I'm missing the second meaning, but I can't find anywhere in the documentation referring to a second kind of aliasing.

Can someone point me in the right direction?

A value is said to be aliased if there is more than one alias to it. An alias is just a name.

In this code:

let s1: String = "hello".into();
let s2: &String = &s1;

s1 and s2 are aliases of the same String value; therefore, the String is aliased.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM