7 Matching Annotations
  1. Last 7 days
    1. These ampersands represent references, and they allow you to refer to some value without taking ownership of it.

      从底层实现看,C++ 引用和 Rust 引用都基于指针,机器码往往一样,所以"底层是指针"这一点相同。但从语义保证看,区别巨大:C++ 引用没有"始终有效"的保证,允许悬空、允许别名修改;Rust 则强制"要么多个只读引用,要么一个可变引用",靠借用检查器编译期强制保证安全。

    2. Unlike a pointer, a reference is guaranteed to point to a valid value of a particular type for the life of that reference.

      C++并不能保证引用有效——依然可以创造悬空引用,这是Rust作为内存安全语言的设计理念。

  2. Jul 2026
    1. if Expressions

      Rust 中 if 是表达式,而不是语句。 Rust 的设计哲学是尽量让语言构造都是表达式,都有值。 C需要三目运算符 ?: 来处理"根据条件返回不同值"的场景

    1. statements don’t evaluate to a value, which is expressed by ()

      Rust 的类型系统要求每一个表达式都有某种类型。如果块没有显式的最终表达式,Rust 给它一个默认值——就是 ()(单元类型)。 表达式必须有值,()也是一种值,只是“什么都没有”这个概念的编码

    1. The program resulted in a runtime error at the point of using an invalid value in the indexing operation. The program exited with an error message and didn’t execute the final println!

      注意是程序自己产生error结束的,而不是靠操作系统(C/C++)