Skip to content

Commit 0935c74

Browse files
author
Colin MacRae
committed
clean main.rs
1 parent 1c343bd commit 0935c74

File tree

2 files changed

+137
-137
lines changed

2 files changed

+137
-137
lines changed

src/main.rs

Lines changed: 0 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +0,0 @@
1-
#![allow(dead_code)]
2-
#![allow(unused_variables)]
3-
// Option<T> is for defining types
4-
// below is how one or more types in a struct
5-
// struct Point<T, V> {
6-
// x:T,
7-
// y:V,
8-
// }
9-
10-
struct Point {
11-
x: f64,
12-
y: f64,
13-
}
14-
15-
struct Line {
16-
start: Point,
17-
end: Point,
18-
}
19-
20-
fn generics() {
21-
// let a:Point<u16, i32> = Point { x: 0.3, y: 4 };
22-
// let b:Point = Point { x: 1.2, y: 3.4};
23-
let a:Point = Point { x: 0.0, y: 4f64 };
24-
let b = Point { x: 1.2, y: 3.4};
25-
26-
let myline = Line { start: a, end: b};
27-
}
28-
29-
fn print_value(x: i32) {
30-
println!("value = {}", x);
31-
}
32-
33-
fn increase(x: &mut i32){
34-
*x += 1;
35-
}
36-
37-
fn product(x: i32, y:i32) -> i32 {
38-
x * y
39-
}
40-
41-
fn functions() {
42-
print_value(33);
43-
44-
let mut z = 1;
45-
increase(&mut z);
46-
println!("z = {}", z);
47-
48-
let a = 3;
49-
let b = 5;
50-
let p = product(3, 5);
51-
println!("{} * {} = {}", a, b, p);
52-
}
53-
54-
impl Line {
55-
fn len(&self) -> f64 {
56-
let dx = self.start.x - self.end.x;
57-
let dy = self.start.y - self.end.y;
58-
(dx * dx + dy * dy).sqrt()
59-
}
60-
}
61-
62-
fn methods() {
63-
let p = Point { x: 3.0, y: 4.0 };
64-
let p2 = Point { x: 5.0, y: 10.0 };
65-
let myline = Line { start: p, end: p2};
66-
67-
println!("length = {}", myline.len());
68-
}
69-
70-
fn say_hello() {
71-
println!("hello");
72-
}
73-
74-
fn closures() {
75-
let sh = say_hello;
76-
sh();
77-
78-
let plus_one = |x: i32| -> i32 { x + 1 };
79-
let a = 6;
80-
println!("{} + 1 = {}", a, plus_one(6));
81-
82-
let mut two = 2;
83-
let plus_two = |x| {
84-
let mut z = x;
85-
z += 2;
86-
z
87-
};
88-
println!("{} + 2 = {}", 3, plus_two(3));
89-
90-
let borrow_two = &mut two;
91-
92-
// T: by value
93-
// T&
94-
// &mut &
95-
// here, plus_three messes with immutable f,
96-
// but print statement would return 12 as
97-
// original var is not affected
98-
// let plus_three = |mut x: i32| { x += 3 };
99-
// here we change f
100-
let plus_three = |x: &mut i32| { *x += 3 };
101-
let mut f = 12;
102-
plus_three(&mut f);
103-
println!("f = {}", f);
104-
}
105-
106-
fn is_even(x:i32) -> bool {
107-
x % 2 == 0
108-
}
109-
110-
fn hof() {
111-
// iterative programming
112-
let limit = 500;
113-
let mut sum = 0;
114-
for i in 0.. {
115-
let isq = i*i;
116-
117-
if isq > limit { break; }
118-
else if is_even(isq) { sum += isq }
119-
}
120-
println!("loop sum = {}", sum);
121-
122-
// more functional programming using HOFs
123-
let sum2 =
124-
(0..).map(|x| x*x)
125-
.take_while(|&x| x <= limit)
126-
.filter(|&x| is_even(x))
127-
.fold(0, |sum,x| sum + x);
128-
println!("hof sum = {}", sum2);
129-
}
130-
131-
fn main() {
132-
// generics();
133-
// functions();
134-
// methods();
135-
// closures();
136-
hof();
137-
}

src/main3.rs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#![allow(dead_code)]
2+
#![allow(unused_variables)]
3+
// Option<T> is for defining types
4+
// below is how one or more types in a struct
5+
// struct Point<T, V> {
6+
// x:T,
7+
// y:V,
8+
// }
9+
10+
struct Point {
11+
x: f64,
12+
y: f64,
13+
}
14+
15+
struct Line {
16+
start: Point,
17+
end: Point,
18+
}
19+
20+
fn generics() {
21+
// let a:Point<u16, i32> = Point { x: 0.3, y: 4 };
22+
// let b:Point = Point { x: 1.2, y: 3.4};
23+
let a:Point = Point { x: 0.0, y: 4f64 };
24+
let b = Point { x: 1.2, y: 3.4};
25+
26+
let myline = Line { start: a, end: b};
27+
}
28+
29+
fn print_value(x: i32) {
30+
println!("value = {}", x);
31+
}
32+
33+
fn increase(x: &mut i32){
34+
*x += 1;
35+
}
36+
37+
fn product(x: i32, y:i32) -> i32 {
38+
x * y
39+
}
40+
41+
fn functions() {
42+
print_value(33);
43+
44+
let mut z = 1;
45+
increase(&mut z);
46+
println!("z = {}", z);
47+
48+
let a = 3;
49+
let b = 5;
50+
let p = product(3, 5);
51+
println!("{} * {} = {}", a, b, p);
52+
}
53+
54+
impl Line {
55+
fn len(&self) -> f64 {
56+
let dx = self.start.x - self.end.x;
57+
let dy = self.start.y - self.end.y;
58+
(dx * dx + dy * dy).sqrt()
59+
}
60+
}
61+
62+
fn methods() {
63+
let p = Point { x: 3.0, y: 4.0 };
64+
let p2 = Point { x: 5.0, y: 10.0 };
65+
let myline = Line { start: p, end: p2};
66+
67+
println!("length = {}", myline.len());
68+
}
69+
70+
fn say_hello() {
71+
println!("hello");
72+
}
73+
74+
fn closures() {
75+
let sh = say_hello;
76+
sh();
77+
78+
let plus_one = |x: i32| -> i32 { x + 1 };
79+
let a = 6;
80+
println!("{} + 1 = {}", a, plus_one(6));
81+
82+
let mut two = 2;
83+
let plus_two = |x| {
84+
let mut z = x;
85+
z += 2;
86+
z
87+
};
88+
println!("{} + 2 = {}", 3, plus_two(3));
89+
90+
let borrow_two = &mut two;
91+
92+
// T: by value
93+
// T&
94+
// &mut &
95+
// here, plus_three messes with immutable f,
96+
// but print statement would return 12 as
97+
// original var is not affected
98+
// let plus_three = |mut x: i32| { x += 3 };
99+
// here we change f
100+
let plus_three = |x: &mut i32| { *x += 3 };
101+
let mut f = 12;
102+
plus_three(&mut f);
103+
println!("f = {}", f);
104+
}
105+
106+
fn is_even(x:i32) -> bool {
107+
x % 2 == 0
108+
}
109+
110+
fn hof() {
111+
// iterative programming
112+
let limit = 500;
113+
let mut sum = 0;
114+
for i in 0.. {
115+
let isq = i*i;
116+
117+
if isq > limit { break; }
118+
else if is_even(isq) { sum += isq }
119+
}
120+
println!("loop sum = {}", sum);
121+
122+
// more functional programming using HOFs
123+
let sum2 =
124+
(0..).map(|x| x*x)
125+
.take_while(|&x| x <= limit)
126+
.filter(|&x| is_even(x))
127+
.fold(0, |sum,x| sum + x);
128+
println!("hof sum = {}", sum2);
129+
}
130+
131+
fn main() {
132+
// generics();
133+
// functions();
134+
// methods();
135+
// closures();
136+
hof();
137+
}

0 commit comments

Comments
 (0)