Skip to content
gqlxj1987's Blog
Go back

borrow checker

Edit page

原文链接

fn main() {
  let mut x: i32 = 22;
  let mut v: Vec<&i32> = vec![];
  let r: &mut Vec<&i32> = &mut v;
  let p: &i32 = &x; // 1. `x` is borrowed here to create `p`
  r.push(p);        // 2. `p` is stored into `v`, but through `r`
  x += 1;           // <-- Error! can't mutate `x` while borrowed
  take(v);          // 3. the reference to `x` is later used here
}

fn take<T>(p: T) { .. }

if you have a mutable loan like r = &mut v, then you can only access the value v through the reference r. Accessing v directly in any way – read, write, or move – would invalidate the loan.

Datalog rules

differential-dataflow

Differential dataflow is a data-parallel programming framework designed to efficiently process large volumes of data and to quickly respond to arbitrary changes in input collections.

using familiar operators like map, filter, join, and group.

// create a degree counting differential dataflow
let (mut input, probe) = worker.dataflow(|scope| {

    // create edge input, count a few ways.
    let (input, edges) = scope.new_collection();

    let out_degr_distr =
    edges.map(|(src, _dst)| src)    // extract source
         .count();                  // count occurrences of source
         .map(|(_src, deg)| deg)    // extract degree
         .count();                  // count occurrences of degree

    // show us something about the collection, notice when done.
    let probe =
    out_degr_distr
        .inspect(|x| println!("observed: {:?}", x))
        .probe();

    (input, probe)
});

subsets relationships

fn main() {
  let mut x: i32 = 22;

  let mut v: Vec<&'0 i32> = vec![];
  
  let r: &'1 mut Vec<&'2 i32> = &'3 mut v;
  // requires: &'3 mut Vec<&'0 i32> <: &'1 mut Vec<&'2 i32>
  //        => '3: '1, '0: '2, '2: '0

  let p: &'5 i32 = &'4 x;
  // requires: &'4 i32 <: &'5 i32
  //        => '4: '5

  r.push(p);
  // requires: &'5 i32 <: &'2 i32
  //        => '5: '2
  
  x += 1;

  take::<Vec<&'6 i32>>(v);
  // requires: Vec<&'0 i32> <: Vec<&'6 i32>
  //        => '0: '6
}

base_subset概念

This input is defined for each borrow expression (e.g., &x or &mut v) in the program.


Edit page
Share this post on:

Previous Post
Q-learning
Next Post
2021计划