Skip to content

Commit bbba6b3

Browse files
committed
added more examples from lectures
1 parent a7d0066 commit bbba6b3

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,8 @@ in a controlled repository now.
5151
* `nullptr.rs`: null pointer example
5252
* `c++-ffi/`: example of FFI between C, C++ and Rust
5353
* `ownership-demo/`: example of ownership and borrowing stuff in C and Rust
54+
* `rust-winter2022/`: examples from Rust class Winter 2022
55+
* `struct-example.rs`: example of Rust struct
56+
* `enum-example.rs`: example of Rust enum
57+
* `closure-example.rs`: example of Rust closure
58+
* `iterator-example.rs`: example of Rust iterator

closure-example.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn sum_off(c: u32) -> u32 {
2+
(1u32..=3).map(|n| { n * n + c }).sum::<u32>()
3+
}
4+
5+
fn main() {
6+
println!("{}", sum_off(7));
7+
}

enum-example.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#[derive(Debug, Clone, Copy)]
2+
enum Color {
3+
Red,
4+
Green,
5+
Blue,
6+
Custom(u8, u8, u8),
7+
}
8+
use Color::*;
9+
10+
fn main() {
11+
let color = Custom(127,127,0);
12+
let color_name = match color {
13+
Red => "#ff0000".to_string(),
14+
Green => "#00ff00".to_string(),
15+
Blue => "#0000ff".to_string(),
16+
Custom(r, g, b) => format!("#{:02x}{:02x}{:02x}", r, g, b),
17+
};
18+
println!("{}", color_name);
19+
}

generics-example.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#[derive(Debug, Clone)]
2+
struct P<T>(T, T);
3+
4+
impl<T> P<T> {
5+
fn first(&self) -> T
6+
where T: Clone
7+
{
8+
self.0.clone()
9+
}
10+
11+
fn sum(&self) -> T
12+
where T: std::ops::Add<Output = T>, T: Clone
13+
{
14+
self.0.clone() + self.1.clone()
15+
}
16+
17+
fn pair(&self) -> (Self, Self)
18+
where Self: Clone
19+
{
20+
(self.clone(), self.clone())
21+
}
22+
}
23+
24+
#[derive(Debug, Clone)]
25+
struct Eg;
26+
27+
fn main() {
28+
let p = P(1u32, 2);
29+
println!("{}", p.sum());
30+
let p = P("hello", "world");
31+
println!("{}", p.first());
32+
let p = P(Eg, Eg);
33+
println!("{:?}", p.pair());
34+
}

rust-winter2022/class-2022-01-05.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let v: Vec<u8> = vec![1, 2];
3+
let mut it = v.into_iter().rev();
4+
while let Some(val) = it.next() {
5+
println!("{}", val);
6+
}
7+
}

struct-example.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#[derive(Debug, Clone)]
2+
struct Point2D(i64, i64);
3+
4+
impl Point2D {
5+
fn offset_point2d(&mut self, (x_off, y_off): (i64, i64)) {
6+
self.0 += x_off;
7+
self.1 += y_off;
8+
}
9+
}
10+
11+
fn main() {
12+
let origin = Point2D(0, 0);
13+
let mut my_point = origin.clone();
14+
my_point.offset_point2d((1, 1));
15+
println!("origin = {:?}; mypoint = {:?}", origin, my_point);
16+
}

0 commit comments

Comments
 (0)