DevilKing's blog

冷灯看剑,剑上几分功名?炉香无需计苍生,纵一穿烟逝,万丈云埋,孤阳还照古陵

0%

borrow checker

原文链接

1
2
3
4
5
6
7
8
9
10
11
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.