Rust中的<-符号是什么? [英] What is the <- symbol in Rust?

查看:366
本文介绍了Rust中的<-符号是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rust中的<-运算符/表达式是什么?您可以在此处找到该符号.

What is the <- operator/expression in Rust? You can find the symbol here.

我碰巧正在看一个页面,描述了Rust中的表达式和操作.我没有用Rust编程,所以我问了一位亲Rust的朋友,这个符号是什么,但即使他也不知道它是什么.

I happened to be looking at a page describing expressions and operations in Rust. I do not program in Rust, so I asked a friend who is pro-Rust what this symbol is but even he doesn't know what it is.

推荐答案

<-运算符不是稳定Rust的一部分.至少还没有.

The <- operator is not part of stable Rust. At least not yet.

有一个 RFC ,其中提出了涉及<-的语法以编写新的对象直接指向内存中的特定位置,以替代

There is an RFC which proposes syntax involving <- for writing new objects directly to specific places in memory, as an alternative to another RFC, which proposes in. This is a generalisation of the (currently unstable) box syntax, which lets you allocate directly to the heap, without a temporary stack allocation.

当前,没有使用unsafe代码的方法是这样的,通常您需要首先在堆栈上进行分配.在此RFC 是相关RFC链中的第一个,并提供了背景动机,但关键原因是:

Currently, there isn't a way to do that without using unsafe code, and often you'll need to allocate on the stack first. There's a discussion of the underlying problem in this RFC which is the first of a chain of related RFCs and gives the background motivation, but the key reasons are:

  • 与期望将对象写入特定内存地址的硬件一起使用.您现在可以在Rust中不安全地执行此操作,但如果SDK可以为此提供安全且高性能的API,那就更好了.
  • 直接写入堆的预分配部分要快得多,而不是每次都分配新的内存.
  • 为新对象分配内存时,直接在堆上执行此操作要比先分配在堆栈上然后进行克隆或移动要快.

在C ++中,有一个称为"placement new"的功能,该功能通过让您向new提供一个参数来实现,该参数是开始写入的现有指针.例如:

In C++, there is a feature called "placement new", which accomplishes this by letting you supply a parameter to new, which is an existing pointer at which to start writing. For example:

// For comparison, a "normal new", allocating on the heap
string *foo = new string("foo");

// Allocate a buffer
char *buffer = new char[100];
// Allocate a new string starting at the beginning of the buffer 
string *bar = new (buffer) string("bar");

据我所知,上面的C ++示例在带有<-的Rust中可能看起来像这样:

From what I can gather, the above C++ example might look like something like this in Rust with <-:

// Memory allocated on heap (with temporary stack allocation in the process)
let foo = Box::new(*b"foo"); 
// Or, without the stack allocation, when box syntax stabilises:
let foo = box *b"foo";

// Allocate a buffer
let mut buffer = box [0u8; 100];
// Allocate a new bytestring starting at the beginning of the buffer 
let bar = buffer[0..3] <- b"bar";

即使实现了展示位置功能,我也不希望此 exact 代码按原样编译.但是请注意,Rust当前无法完成最后一行 trying 的工作:直接在缓冲区的开头分配b"bar",而不先分配堆栈.目前在Rust中,还没有办法做到这一点.即使unsafe代码在这里也无济于事.您仍然必须先在堆栈上分配,然后将其克隆到缓冲区:

I wouldn't expect this exact code to compile as-is, even if the placement feature was implemented. But notice it is not currently possible in Rust to do what the last line is trying to do: allocate b"bar" directly at the start of the buffer, without allocating on the stack first. In Rust right now, there just isn't a way to do that. Even unsafe code doesn't help you here. You'd still have to allocate on the stack first and then clone it to the buffer:

// Note that b"bar" is allocated first on the stack before being copied
// into the buffer
buffer[0..3].clone_from_slice(&b"bar"[0..3]);
let bar = &buffer[0..3];

box语法在这里也无济于事.那将分配新的堆内存,然后您仍然必须将数据复制到缓冲区.

And box syntax wouldn't help here either. That would allocate new heap memory, and you'd still then have to copy the data to the buffer.

对于在堆上分配新对象时避免临时分配堆栈的更简单情况,box语法将在稳定时解决该问题. Rust在将来的某个时候将需要解决更复杂的情况,但是还不确定<-是将要出现的语法.

For the simpler case of avoiding temporary stack allocation when allocating new objects on the heap, the box syntax will solve that when it stabilises. Rust will need to solve the more complicated cases at some point in the future, but it it is not yet certain that <- is the syntax that will emerge.

这篇关于Rust中的&lt;-符号是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆