simple/
simple.rs

1use rt_vec::RtVec;
2
3struct A(u32);
4
5fn main() {
6    let mut rt_vec = RtVec::new();
7
8    rt_vec.push(A(1));
9    rt_vec.push(A(2));
10
11    // We can validly have two mutable borrows from the `RtVec` map!
12    let mut a = rt_vec.borrow_mut(0);
13    let mut b = rt_vec.borrow_mut(1);
14    a.0 = 2;
15    b.0 = 3;
16
17    // We need to explicitly drop the A and B borrows, because they are runtime
18    // managed borrows, and rustc doesn't know to drop them before the immutable
19    // borrows after this.
20    drop(a);
21    drop(b);
22
23    // Multiple immutable borrows to the same value are valid.
24    let a_0 = rt_vec.borrow(0);
25    let _a_1 = rt_vec.borrow(0);
26    let b = rt_vec.borrow(1);
27
28    println!("A: {}", a_0.0);
29    println!("B: {}", b.0);
30
31    // Trying to mutably borrow a value that is already borrowed (immutably
32    // or mutably) returns `Err`.
33    let a_try_borrow_mut = rt_vec.try_borrow_mut(0);
34    let exists = if a_try_borrow_mut.is_ok() {
35        "Ok(..)"
36    } else {
37        "Err"
38    };
39
40    println!("a_try_borrow_mut: {}", exists); // prints "Err"
41}