Skip to content

Commit a682663

Browse files
committed
updated some of the examples
1 parent 4fbdf17 commit a682663

File tree

6 files changed

+123
-58
lines changed

6 files changed

+123
-58
lines changed

addy.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub trait Numy {
1010
lhs + rhs
1111
}
1212

13-
fn render(self) -> Self::Rendering;
13+
fn render(&self) -> Self::Rendering;
1414
}
1515

1616
struct U64String(String);
@@ -29,15 +29,15 @@ impl std::ops::Add for U64String {
2929
impl Numy for U64String {
3030
type Rendering = String;
3131

32-
fn render(self) -> Self::Rendering {
33-
self.0
32+
fn render(&self) -> Self::Rendering {
33+
self.0.clone()
3434
}
3535
}
3636

3737
impl Numy for f64 {
3838
type Rendering = String;
3939

40-
fn render(self) -> Self::Rendering {
40+
fn render(&self) -> Self::Rendering {
4141
self.to_string()
4242
}
4343
}
@@ -49,11 +49,12 @@ impl Numy for u64 {
4949
lhs + rhs + 1
5050
}
5151

52-
fn render(mut self) -> Self::Rendering {
52+
fn render(&self) -> Self::Rendering {
53+
let mut value = *self;
5354
let mut result = Vec::new();
54-
while self > 0 {
55-
result.push((self % 10) as u8);
56-
self /= 10;
55+
while value > 0 {
56+
result.push((value % 10) as u8);
57+
value /= 10;
5758
}
5859
result.reverse();
5960
result

bno-salad.rs

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
// From Blandy & Orendorff's *Programming Rust*
22

3-
trait Vegetable {
3+
pub trait Vegetable {
44
fn desc(&self) -> &'static str;
55
}
66

77
#[derive(Clone, Copy)]
8-
struct Lettuce;
8+
pub struct Lettuce;
99
impl Vegetable for Lettuce {
1010
fn desc(&self) -> &'static str {
11-
"lettuce"
11+
self.desc()
1212
}
1313
}
1414

1515
#[derive(Clone, Copy)]
16-
struct Tomato;
16+
pub struct Tomato;
1717
impl Vegetable for Tomato {
1818
fn desc(&self) -> &'static str {
19-
"tomato"
19+
self.desc()
2020
}
2121
}
2222

23-
struct GenericSalad<T: Vegetable> {
23+
pub struct GenericSalad<T: Vegetable> {
2424
veggies: Vec<T>,
2525
}
2626

@@ -35,46 +35,102 @@ impl<T: Vegetable> GenericSalad<T> {
3535
self.veggies.push(v);
3636
}
3737

38-
fn display(&self) {
38+
pub fn display(&self) {
3939
for v in &self.veggies {
4040
println!("{}", v.desc());
4141
}
4242
}
4343
}
4444

45-
struct TraitSalad<'a> {
46-
veggies: Vec<&'a dyn Vegetable>,
45+
pub struct TraitSalad {
46+
veggies: Vec<Box<dyn Vegetable>>,
4747
}
4848

49-
impl<'a> TraitSalad<'a> {
49+
impl TraitSalad {
5050
fn new() -> Self {
5151
TraitSalad {
5252
veggies: Vec::new(),
5353
}
5454
}
5555

56-
fn add(&mut self, v: &'a dyn Vegetable) {
56+
fn add(&mut self, v: Box<dyn Vegetable>) {
5757
self.veggies.push(v);
5858
}
5959

60-
fn display(&self) {
60+
pub fn display(&self) {
6161
for v in &self.veggies {
6262
println!("{}", v.desc());
6363
}
6464
}
6565
}
6666

67-
fn main() {
67+
impl Lettuce {
68+
fn desc(&self) -> &'static str {
69+
"lettuce"
70+
}
71+
}
72+
73+
impl Tomato {
74+
fn desc(&self) -> &'static str {
75+
"tomato"
76+
}
77+
}
78+
79+
enum SaladVegetable {
80+
SaladLettuce,
81+
SaladTomato,
82+
SaladCucumber,
83+
}
84+
use SaladVegetable::*;
85+
86+
pub struct EnumSalad {
87+
veggies: Vec<SaladVegetable>,
88+
}
89+
90+
impl EnumSalad {
91+
fn new() -> Self {
92+
EnumSalad {
93+
veggies: Vec::new(),
94+
}
95+
}
96+
97+
fn add(&mut self, v: SaladVegetable) {
98+
self.veggies.push(v);
99+
}
100+
101+
pub fn display(&self) {
102+
for v in &self.veggies {
103+
match v {
104+
SaladLettuce => println!("lettuce"),
105+
SaladTomato => println!("tomato"),
106+
SaladCucumber => println!("cucumber"),
107+
}
108+
}
109+
}
110+
}
111+
112+
pub fn make_me_a_generic_lettuce_salad<T: Vegetable + Clone>(veggie: T) ->
113+
GenericSalad<T>
114+
{
68115
let mut generic_salad = GenericSalad::new();
69-
for v in vec![Lettuce, Lettuce] {
116+
for v in vec![veggie.clone(), veggie.clone()] {
70117
generic_salad.add(v);
71118
}
72-
generic_salad.display();
73-
74-
println!();
119+
generic_salad
120+
}
75121

122+
pub fn make_me_a_trait_salad() -> TraitSalad {
76123
let mut trait_salad = TraitSalad::new();
77-
trait_salad.add(&Lettuce);
78-
trait_salad.add(&Tomato);
79-
trait_salad.display();
124+
trait_salad.add(Box::new(Lettuce));
125+
trait_salad.add(Box::new(Tomato));
126+
trait_salad
127+
}
128+
129+
pub fn make_me_a_enum_salad() -> EnumSalad {
130+
let mut enum_salad = EnumSalad::new();
131+
enum_salad.add(SaladLettuce);
132+
enum_salad.add(SaladTomato);
133+
enum_salad.add(SaladTomato);
134+
enum_salad.add(SaladCucumber);
135+
enum_salad
80136
}

matmul.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,49 @@
11
use std::ops::Mul;
22

33
#[derive(Debug, Clone, Copy)]
4-
struct Matrix3([[u64;3];3]);
4+
struct Matrix<const N: usize>([[u64;N];N]);
55

6-
impl Mul for Matrix3 {
7-
type Output = Matrix3;
6+
impl<const N: usize> Mul for Matrix<N> {
7+
type Output = Matrix<N>;
88

9-
fn mul(self, Matrix3(rhs): Self) -> Self {
10-
let Matrix3(lhs) = self;
11-
let mut result = [[0;3];3];
12-
for i in 0..3 {
13-
for j in 0..3 {
14-
for k in 0..3 {
9+
fn mul(self, Matrix(rhs): Self) -> Self {
10+
let Matrix(lhs) = self;
11+
let mut result = [[0;N];N];
12+
for i in 0..N {
13+
for j in 0..N {
14+
for k in 0..N {
1515
result[i][k] += lhs[i][j] * rhs[j][k];
1616
}
1717
}
1818
}
19-
Matrix3(result)
19+
Matrix(result)
2020
}
2121
}
2222

23-
impl PartialEq for Matrix3 {
24-
fn eq(&self, rhs: &Matrix3) -> bool {
23+
impl<const N: usize> PartialEq for Matrix<N> {
24+
fn eq(&self, rhs: &Matrix<N>) -> bool {
2525
self.0 == rhs.0
2626
}
2727
}
2828

29-
const TTT: Matrix3 = Matrix3(
29+
const TTT: Matrix<3> = Matrix(
3030
[[1,2,3],
3131
[4,5,6],
3232
[7,8,9]]
3333
);
3434

35-
const ID: Matrix3 = Matrix3(
35+
const ID: Matrix<3> = Matrix(
3636
[[1,0,0],
3737
[0,1,0],
3838
[0,0,1]]
3939
);
4040

41-
const MAGIC: Matrix3 = Matrix3(
41+
const ID2: Matrix<2> = Matrix(
42+
[[1,0],
43+
[0,1]]
44+
);
45+
46+
const MAGIC: Matrix<3> = Matrix(
4247
[[6,1,8],
4348
[7,5,3],
4449
[2,9,4]]
@@ -48,5 +53,6 @@ fn main() {
4853
assert_eq!(TTT * ID, TTT);
4954
assert_eq!(ID * TTT, TTT);
5055
assert!(TTT * MAGIC != MAGIC * TTT);
56+
assert_eq!(ID2 * ID2, ID2);
5157
println!("{:?}", TTT * TTT);
5258
}

phantom.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::marker::PhantomData;
22

3-
#[derive(Debug)]
3+
#[derive(Debug, PartialEq)]
44
struct Hash<T> {
55
h: u128,
66
p: PhantomData<T>,
@@ -30,7 +30,9 @@ fn main() {
3030
println!("{:?}", h1);
3131
let h2 = Hash::hash(1u64);
3232
println!("{:?}", h2);
33+
let h3 = Hash::hash((0i32, 1i32));
34+
println!("{:?}", h1);
3335
// Can't even compare these, since they are of
3436
// different type.
35-
// println!("{}", h1 == h2);
37+
println!("{}", h1 == h2);
3638
}

point.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Point {
2121
}
2222

2323
fn collapse(self) -> i64 {
24-
self.x + self.y
24+
self.x - self.y
2525
}
2626
}
2727

top.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ impl<T: fmt::Debug> fmt::Debug for Top<'_, T> {
2222
}
2323

2424
// Can parameterize a function to make it able to take a specified type of data.
25-
fn make_top<T>(count: usize, order: fn(&T, &T) -> Ordering, vals: &[T]) -> Top<'_, T> {
26-
let mut elems: Vec<&T> = vals.iter().collect();
27-
elems.sort_by(|&v1, &v2| order(v1, v2));
28-
if count < elems.len() {
29-
elems.truncate(count);
30-
}
31-
Top { count, order, elems }
32-
}
33-
3425
// Must parameterize an impl to match its type.
3526
impl<'a, T> Top<'a, T> {
27+
fn new(count: usize, order: fn(&T, &T) -> Ordering, vals: &[T]) -> Top<'_, T> {
28+
let mut elems: Vec<&T> = vals.iter().collect();
29+
elems.sort_by(|&v1, &v2| order(v1, v2));
30+
if count < elems.len() {
31+
elems.truncate(count);
32+
}
33+
Top { count, order, elems }
34+
}
35+
3636
fn update(&mut self, val: &'a T) {
3737
self.elems.push(val);
3838
let order = self.order;
@@ -45,9 +45,9 @@ impl<'a, T> Top<'a, T> {
4545
}
4646

4747
fn main() {
48-
let vals = [9, 7, 3, 1, 4, 6, 8, 2, 5];
49-
let mut top = make_top(3, |x, y| x.cmp(y), &vals);
48+
let vals = ['d', 'b', 'c', 'a'];
49+
let mut top = Top::new(3, |x, y| x.cmp(y), &vals);
5050
println!("{:?}", top);
51-
top.update(&0);
51+
Top::update(&mut top, &'X');
5252
println!("{:?}", top);
5353
}

0 commit comments

Comments
 (0)