<- 运算符不是稳定版 Rust 的一部分。至少现在还没有。
有一个RFC 提出了涉及<- 的语法,用于将新对象直接写入内存中的特定位置,作为another RFC 的替代方案,它提出了in。这是(目前不稳定的)box 语法的概括,它允许您直接分配给堆,而无需临时分配堆栈。
目前,如果不使用unsafe 代码,则无法做到这一点,而且通常您需要先在堆栈上进行分配。 this RFC 中讨论了潜在问题,这是相关 RFC 链中的第一个,并给出了背景动机,但关键原因是:
- 使用期望将对象写入特定内存地址的硬件。您现在可以在 Rust 中不安全地执行此操作,但如果 SDK 可以为此提供安全且高性能的 API,那就更好了。
- 直接写入堆的预分配部分比每次都分配新内存更快。
- 为新对象分配内存时,直接在堆上执行此操作会更快,而不是先在堆栈上分配然后克隆或移动。
在 C++ 中,有一个名为“placement new”的功能,它通过让您向new 提供一个参数来实现这一点,这是一个开始写入的现有指针。例如:
// 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 中可能看起来像这样:
// 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";
我不希望这个确切代码按原样编译,即使实现了放置功能。但请注意,目前在 Rust 中不可能执行最后一行 正在尝试 执行的操作:直接在缓冲区的开头分配 b"bar",而不是先在堆栈上分配。现在在 Rust 中,没有办法做到这一点。即使unsafe 代码在这里也无济于事。您仍然必须先在堆栈上分配,然后将其克隆到缓冲区:
// 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 语法在这里也无济于事。这将分配新的堆内存,然后您仍然必须将数据复制到缓冲区。
对于在堆上分配新对象时避免临时堆栈分配的更简单情况,box 语法将在稳定时解决该问题。 Rust 将需要在未来某个时候解决更复杂的情况,但尚不确定<- 是否会出现。