Rust 如何提供移动语义? [英] How does Rust provide move semantics?
问题描述
Rust 语言网站声称移动语义是该语言的特征之一.但是我看不到 Rust 中是如何实现移动语义的.
Rust 盒子是唯一使用移动语义的地方.
let x = Box::new(5);让 y:Box= x;//x 被移动"
上面的 Rust 代码可以用 C++ 写成
auto x = std::make_unique();自动 y = std::move(x);//注意显式移动
据我所知(如果我错了,请纠正我),
- Rust 根本没有构造函数,更不用说移动构造函数了.
- 不支持右值引用.
- 无法使用右值参数创建函数重载.
Rust 如何提供移动语义?
我认为当来自 C++ 时,这是一个非常常见的问题.在 C++ 中,当涉及到复制和移动时,您正在明确地做所有事情.该语言是围绕复制和引用而设计的.在 C++11 中,移动"东西的能力被粘在了那个系统上.另一方面,Rust 重新开始.
<小时><块引用>Rust 根本没有构造函数,更不用说移动构造函数了.
您不需要移动构造函数.Rust 移动了所有没有复制构造函数"的东西,也就是没有实现 Copy
特性".
struct A;fn 测试(){让 a = A;让 b = a;让 c = a;//错误,a 被移动}
Rust 的默认构造函数(按照惯例)只是一个名为 new
的关联函数:
struct A(i32);实现{fn new() ->一个 {甲(5)}}
更复杂的构造函数应该有更具表现力的名称.这是 C++ 中的命名构造函数习语
<小时><块引用>不支持右值引用.
它一直是要求的功能,请参阅 RFC 问题 998,但是很可能您要求的是不同的功能:将内容移至函数:
struct A;fn move_to(a: A) {//a 被移到这里,你现在拥有它.}fn 测试(){让 a = A;移动到(一);让 c = a;//错误,a 被移动}
<小时><块引用>
无法使用右值参数创建函数重载.
你可以用特质来做到这一点.
trait 引用 {fn 测试(&self);}特质移动{fn 测试(自我);}结构A;A { 的 impl Reffn 测试(&self){println!("by ref");}}impl 移动 A {fn 测试(自我){println!("按值");}}fn 主(){让 a = A;(&a).test();//打印by ref"一个测试();//打印按值"}
The Rust language website claims move semantics as one of the features of the language. But I can't see how move semantics is implemented in Rust.
Rust boxes are the only place where move semantics are used.
let x = Box::new(5);
let y: Box<i32> = x; // x is 'moved'
The above Rust code can be written in C++ as
auto x = std::make_unique<int>();
auto y = std::move(x); // Note the explicit move
As far as I know (correct me if I'm wrong),
- Rust doesn't have constructors at all, let alone move constructors.
- No support for rvalue references.
- No way to create functions overloads with rvalue parameters.
How does Rust provide move semantics?
I think it's a very common issue when coming from C++. In C++ you are doing everything explicitly when it comes to copying and moving. The language was designed around copying and references. With C++11 the ability to "move" stuff was glued onto that system. Rust on the other hand took a fresh start.
Rust doesn't have constructors at all, let alone move constructors.
You do not need move constructors. Rust moves everything that "does not have a copy constructor", a.k.a. "does not implement the Copy
trait".
struct A;
fn test() {
let a = A;
let b = a;
let c = a; // error, a is moved
}
Rust's default constructor is (by convention) simply an associated function called new
:
struct A(i32);
impl A {
fn new() -> A {
A(5)
}
}
More complex constructors should have more expressive names. This is the named constructor idiom in C++
No support for rvalue references.
It has always been a requested feature, see RFC issue 998, but most likely you are asking for a different feature: moving stuff to functions:
struct A;
fn move_to(a: A) {
// a is moved into here, you own it now.
}
fn test() {
let a = A;
move_to(a);
let c = a; // error, a is moved
}
No way to create functions overloads with rvalue parameters.
You can do that with traits.
trait Ref {
fn test(&self);
}
trait Move {
fn test(self);
}
struct A;
impl Ref for A {
fn test(&self) {
println!("by ref");
}
}
impl Move for A {
fn test(self) {
println!("by value");
}
}
fn main() {
let a = A;
(&a).test(); // prints "by ref"
a.test(); // prints "by value"
}
这篇关于Rust 如何提供移动语义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!