You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

关于拥有Read(R)和Own(O)权限的引用的操作疑问

关于拥有Read(R)和Own(O)权限的引用的操作疑问

Hey there! Let's break down your questions step by step, and first clarify a few key points tying your R/W/O permission model to Rust's actual ownership and borrowing rules.

First, a quick heads-up about your sample code: it won't compile! Rust's borrow checker blocks you from modifying the Vec (via push) while an active immutable reference (&i32) exists to one of its elements. This is a core safety rule—you can have any number of immutable borrows or one mutable borrow, never both at the same time.

1. How to read data from a reference with "R" permission, even though it doesn't own the data?

You don't need to worry about the reference not owning the data—that's exactly what references are designed for! Immutable references (which map to your "R" permission) act as safe, read-only windows into the underlying data. Here's how you use them:

  • Explicit dereferencing: Use the * operator to access the value the reference points to, just like you did in your code:
    println!("Third element is {}", *num);
    
  • Automatic dereferencing (Deref Coercion): Rust has a handy syntax sugar that lets you skip the * in many cases. For example, println!("Third element is {}", num); works perfectly too—Rust automatically converts the &i32 reference to an i32 value when passing it to println!.
  • You can also use the reference for any other read-only operation: pass it to a function that accepts &i32, compare it to other values (like if num == &5 { ... }), or clone the underlying value with let value = *num; (if the type implements Clone).

2. How to "move" a reference?

First, let's clarify: when we talk about moving a reference, we're talking about moving the reference itself (a tiny, pointer-sized value), not the underlying data it points to. References never own the underlying data—only the original owner (like the Vec in your example) can move that.

How "moving" works depends on the type of reference:

  • Immutable references (&T): These implement the Copy trait, which means when you assign them to another variable or pass them to a function, you're actually copying the reference (not moving it). The original reference remains fully usable. For example:
    let v = vec![1, 2, 3];
    let num = &v[2];
    let num2 = num; // Copies the reference, num is still valid
    println!("{} and {}", num, num2); // This runs without issues
    
    This is probably what your "O" permission refers to—you can freely duplicate the reference as needed, with zero overhead.
  • Mutable references (&mut T): These do not implement Copy, so assigning them to another variable or passing them to a function counts as a move. After the move, the original reference variable is no longer usable (Rust will throw a compile error if you try to use it). Example:
    let mut v = vec![1, 2, 3];
    let mut_num = &mut v[2];
    let mut_num2 = mut_num; // Moves mut_num to mut_num2
    // println!("{}", mut_num); // Error: mut_num has been moved
    

To wrap up: Your R/O/W model is a useful abstraction, but in Rust terms:

  • "R" permission maps to immutable references (&T) for read-only access
  • "W" permission maps to mutable references (&mut T) for modifying data
  • "O" permission for references refers to the ability to copy or move the reference itself, not ownership of the underlying data (which always stays with the original owner like the Vec).

火山引擎 最新活动