Rust–:没有借用检查器的Rust
Rust–: Rust without the borrow checker

原始链接: https://github.com/buyukakyuz/rustmm

## Rustmm:一个没有借用检查器的 Rust 编译器 Rustmm 是一个修改过的 Rust 编译器,它禁用了借用检查器,允许包含传统上无效借用模式的代码编译和运行。这对于试验不安全代码或移植受严格借用检查阻碍的代码库很有用。 预构建的二进制文件可通过简单的安装脚本提供,适用于 macOS (Apple Silicon) 和 Linux (x86_64):`curl -sSL https://raw.githubusercontent.com/buyukakyuz/rustmm/main/install.sh | bash`。然后可以通过 `~/.rustmm/bin/rustc` 访问编译器。 Rustmm 绕过了与在借用后移动值、多个可变引用以及冲突的借用类型相关的错误——这些问题通常会被标准的 Rust 编译器标记。 几个例子展示了这一点,包括涉及已移动值、多个可变借用以及移动后使用的情况。 `examples/` 目录提供了专门设计用于在标准 Rust 下失败,但在 Rustmm 下可以成功编译和运行的代码。它采用 Apache 2.0 和 MIT 许可。

一个名为“Rust–”的新项目旨在提供一种*没有*借用检查器的 Rust 体验,可能简化开发并加速原型设计。该项目在 Hacker News 上引发了关于其实用性和潜在缺点的讨论。 用户质疑禁用借用检查器是否最终需要大量的代码重构才能稍后遵守 Rust 的安全规则。一些人认为它可能是一个用于快速实验的宝贵编译选项,而另一些人则将其比作“带有额外步骤的 C++”。 讨论还涉及了对编译器优化的影响(Rust 的借用检查器使其成为可能)以及在文本编辑器等复杂应用中实现零动态分配的挑战。有趣的是,独立的 Rust 借用检查器被强调为构建替代编译器的有用工具。该项目的许可和一个有趣的“NSFW”标签也引起了关注。
相关文章

原文

A modified Rust compiler with the borrow checker disabled. This allows code that would normally violate Rust's borrowing rules to compile and run successfully.

Pre-built binaries for macOS (Apple Silicon) and Linux (x86_64):

curl -sSL https://raw.githubusercontent.com/buyukakyuz/rustmm/main/install.sh | bash

Use:

~/.rustmm/bin/rustc your_code.rs

To build from source, see BUILDING.md.

Examples: Before vs After

Normal Rust:

fn main() {
    let a = String::from("hello");
    let b = a;
    println!("{a}");
}

Error in normal Rust:

error[E0382]: borrow of moved value: `a`
 --> test.rs:4:16
  |
2 |     let a = String::from("hello");
  |         - move occurs because `a` has type `String`, which does not implement the `Copy` trait
3 |     let b = a;
  |             - value moved here
4 |     println!("{a}");
  |                ^ value borrowed here after move

Rust--:

fn main() {
    let a = String::from("hello");
    let b = a;
    println!("{a}");  // Works! Prints: hello
}

Example 2: Multiple Mutable References

Normal Rust:

fn main() {
    let mut y = 5;
    let ref1 = &mut y;
    let ref2 = &mut y;
    *ref1 = 10;
    *ref2 = 20;
}

Error in normal Rust:

error[E0499]: cannot borrow `y` as mutable more than once at a time
 --> test.rs:4:16
  |
3 |     let ref1 = &mut y;
  |                ------ first mutable borrow occurs here
4 |     let ref2 = &mut y;
  |                ^^^^^^ second mutable borrow occurs here
5 |     *ref1 = 10;
  |     ---------- first borrow later used here

Rust--:

fn main() {
    let mut y = 5;
    let ref1 = &mut y;
    let ref2 = &mut y;  // Works!
    *ref1 = 10;
    *ref2 = 20;
    println!("{}", y);  // Prints: 20
}

Example 3: Mutable Borrow Then Move

Normal Rust:

fn main() {
    let mut x = vec![1, 2, 3];
    let borrowed = &mut x;
    println!("{:?}", x);  // ERROR: cannot use `x` while mutable borrow exists
}

Rust--:

fn main() {
    let mut x = vec![1, 2, 3];
    let borrowed = &mut x;
    println!("{:?}", x);  // Works! Prints: [1, 2, 3]
}

Example 4: Use After Move in Loop

Normal Rust:

fn main() {
    let s = String::from("test");
    for _ in 0..2 {
        println!("{}", s);  // ERROR: cannot move out of loop
    }
}

Rust--:

fn main() {
    let s = String::from("test");
    for _ in 0..2 {
        println!("{}", s);  // Works! Prints twice
    }
}

Example 5: Conflicting Borrows

Normal Rust:

fn main() {
    let mut num = 42;
    let mut_ref = &mut num;
    let immut_ref = #
    println!("{}", immut_ref);
    println!("{}", mut_ref);
}

Error in normal Rust:

error[E0502]: cannot borrow `num` as immutable because it is also borrowed as mutable
 --> test.rs:4:21
  |
3 |     let mut_ref = &mut num;
  |                   -------- mutable borrow occurs here
4 |     let immut_ref = #
  |                     ^^^^ immutable borrow occurs here
5 |     println!("{}", immut_ref);
6 |     println!("{}", mut_ref);
  |                    ------- mutable borrow later used here

Rust--:

fn main() {
    let mut num = 42;
    let mut_ref = &mut num;
    let immut_ref = #  // Works! No error
    println!("{}", immut_ref);  // Prints: 42
    println!("{}", mut_ref);    // Prints: 0x...
}

The examples/ directory contains code that would fail in standard Rust:

  • 01_move_then_use.rs - E0382: Borrow of moved value
  • 02_multiple_mutable_borrows.rs - E0499: Multiple mutable borrows
  • 03_mutable_borrow_then_move.rs - Mutable borrow then move
  • 04_use_after_move_loop.rs - Use after move in loop
  • 05_move_and_use.rs - Move and use simultaneously
  • 06_conflicting_borrows.rs - E0502: Conflicting borrows
~/.rustmm/bin/rustc examples/01_move_then_use.rs && ./01_move_then_use

Same as Rust - dual licensed under Apache 2.0 and MIT

See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details.

联系我们 contact @ memedata.com