Skip to content

Commit 72f69e7

Browse files
committed
Slides update
1 parent 5c04521 commit 72f69e7

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed
Binary file not shown.

presentations/2025-11-10-local-reasoning-rs/Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

presentations/2025-11-10-local-reasoning-rs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7+
anyhow = "1"

presentations/2025-11-10-local-reasoning-rs/src/main.rs

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
use anyhow::{Ok, Result};
2+
13
// Returns the successor of `x`.
24
// Precondition: `x < INT_MAX`
35

4-
fn f(x: i32) -> i32 {
6+
/* fn f(x: i32) -> i32 {
57
x + 1
6-
}
8+
} */
79
#[derive(Debug)]
810
struct Point {
911
x: i32,
@@ -132,7 +134,79 @@ fn demonstrate_pairs_iterator() {
132134

133135
// fn f(a: &mut Box<i32>) -> () {}
134136

137+
struct EveryNth<'a, T> {
138+
slice: &'a [T],
139+
n: usize,
140+
i: usize,
141+
}
142+
143+
impl<'a, T> Iterator for EveryNth<'a, T> {
144+
type Item = &'a T;
145+
fn next(&mut self) -> Option<Self::Item> {
146+
(self.i < self.slice.len()).then(|| {
147+
let item = &self.slice[self.i];
148+
self.i += self.n;
149+
item
150+
})
151+
}
152+
}
153+
154+
type Type = i32;
155+
156+
fn f() -> Result<Type> {
157+
Ok(1)
158+
}
159+
fn g() -> Result<Type> {
160+
Ok(2)
161+
}
162+
fn h(a: &mut Type) -> Result<()> {
163+
Ok(())
164+
}
165+
166+
fn a(a: &mut Type) -> Result<()> {
167+
let mut x = f()?.g()?;
168+
h(&mut x)?;
169+
h(a)?;
170+
Ok(())
171+
}
172+
173+
fn op(s: String) -> String {
174+
s + "!"
175+
}
176+
177+
struct S {
178+
data: String,
179+
}
180+
135181
fn main() {
182+
let mut s = String::from("Hello, world!");
183+
s = op(s);
184+
185+
let mut s2 = S{data: String::from("Hello, world!")};
186+
s2.data = op(s2.data);
187+
188+
let mut a = vec![3, 2, 1, 0];
189+
let (left, right) = a.split_at_mut(2); // Ensure non-overlapping
190+
right.copy_from_slice(&left);
191+
192+
println!("{:?}", a);
193+
194+
let data = [10, 20, 30, 40, 50, 60];
195+
let iter = EveryNth {
196+
slice: &data,
197+
n: 3,
198+
i: 0,
199+
};
200+
201+
for val in iter {
202+
println!("{}", val); // prints 10, 30, 50
203+
}
204+
205+
/* let mut a = vec![3, 2, 1, 0];
206+
let src = &a[0..2];
207+
let dst = &mut a[2..]; // ERROR
208+
dst.copy_from_slice(src); */
209+
136210
let mut a = vec![0, 1, 2, 3];
137211
let e = a.last().unwrap();
138212
a.clear(); // e is invalid

0 commit comments

Comments
 (0)