1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
---
author: Adam T. Carpenter, Carpenter Tutoring
---
# Computational problem solving with _Rust_
## The study of computer science
```
~~~fsays
Not just for computer scientists
~~~
```
[practice of computing using python](https://www.pearson.com/en-us/subject-catalog/p/practice-of-computing-using-python-the/p200000003329/9780137524839)
[rust book](https://doc.rust-lang.org/book/)
---
# Why computer science?
Computers are "more universally applicable than any other commodity in history."
(Punch & Enbody)
There is no other machine with as many diverse uses as the computer. A
reprogrammable machine is a machine that works for _you_.
## Not just computer programming
- theory of computation
- efficiency
- algorithms and data structures
- parallelism
- software engineering
_Computer programming is a great way to begin exploring these fields_
---
# Difficulty in first-time programming
It's like learning how to write poetry in a foreign language. You need:
- fluency: the vocabulary and grammar to read and write the language
- ability: the skill to rhyme, write in verse, _make poetry_
Learning to program has similar roadblocks. You need:
- syntax and semantics: the structure of Rust as a language
- problem solving: the skill to transform your problem into a solution
---
# Good programs are essays
Programming is writing how _you_ think a problem should be solved.
A program is your thoughts!
## 1. Think before you program
Writing a program should describe your thoughts well.
Not just for computers, but for other human beings! You're going to read your
program over and over again. Eventually you'll inevitably write programs in a
group. Then others will need to read it too.
## 2. A program is a human-readable essay on problem solving (that also runs on a computer)
---
# The promise
A program, or an essay on problem-solving, has impact because it can be executed
on a computer. Your problem-solving thoughts
- are executable
- are repeatable
- are independent of you
Programming is a leap forward in the way the printing press was hundreds of
years ago.
---
# Choosing a language
The variety of languages is enormous, each one with a specific intended purpose.
## Why Rust? "performance, reliability, productivity"
- useful types and memory guarantees
- makes it easy to write concurrent programs
- has a friendly compiler and helpful borrow checker
- provides good documentation
- offers great standard library and third-party libraries
Thanks to middle school math, most folks have the prerequisite knowledge to
understand variables, functions, and types.
## Why not C, Java, Python, etc.?
Some of these languages force you to think about computer organization. Others
may direct you into an object-oriented programming mindset. Still others promote
dynamic type conversion or make it easy to dereference null values.
The most important reason for using Rust is it helps prevent you from making
mistakes, regardless of your skill level.
---
# Is Rust the best language?
If you don't already know you'll soon learn there is no "best language."
All languages compromise on something; Each has its strengths and weaknesses.
Rust is a good, _general-purpose_ programming language with _broad_
_applications_.
Once you've gotten started, you'll be better equipped to explore other languages
that may be better suited to solving your specific problems.
---
# Computation and computers
_Computation_ is the manipulation of data by humans or machines
...be that data numbers, letters, or other symbols.
A _computer_ is something that does computation
Note this does not specify _how_ the computation is accomplished. However, there
are some things that every computer needs in order to do its job.
- accept data as input
- manipulate data (do computation on the input)
- output data
---
# The modern computer
Many components drive the function of a typical home computer.
```
~~~graph-easy --as boxart
[Processor] -> [Memory]
[Memory] -> [Disk]
[Memory] -> [Input]
[Memory] -> [Output]
[Memory] -> [Network]
~~~
```
Inputs can be mouse and keyboard, or touch interfaces. Outputs may be a video
monitor or a printer. The network itself is a glorified input/output device,
allowing for communication between and among groups of computers.
---
# The origin of the modern computer
_Computer_ used to be a job description for human beings!
General-purpose, reprogrammable, electronic digital computers first appeared in
the 20th century.
The basis of every digital computer is an on-off switch. The technology behind
these "switches" has evolved from mechanical relays to vacuum tubes to
transistors.
_Transistors_, the tiny electronic switches powering modern computers, are
either on or off. Just like a light switch, when turned on, they allow
electricity to flow. Using switches to power other switches, you can create
circuits that represent logic.
---
# Example: a three-pole light switch
```
~~~graph-easy --as boxart
[hall switch] .. off ..> [ceiling lamp]
[den switch] - on -> [ceiling lamp]
[hall switch] <-> [den switch]
~~~
```
---
# Example: a three-pole light switch
```
~~~graph-easy --as boxart
[hall switch] - on -> [*ceiling lamp*] { border: bold; }
[den switch] - on -> [*ceiling lamp*] { border: bold; }
[hall switch] <-> [den switch]
~~~
```
Note: this is actually an XNOR gate, but we'll talk more about logical operators
later.
---
# Example: a three-pole light switch
## Replace AC power with truth
Off: false, 0, "no"
On: true, 1, "yes"
These are the building blocks of every digital computer in the world today.
---
# Representing numbers
> Bits and bytes of information, turning darkness to light.
(_Bits and Bytes_, 1983)
So how do we use billions of tiny transistors, on-off switches, or 0s and 1s
into English essays, photographs, or 3D animated feature films?
---
# Representing numbers
Probably before grade school, you learned to count from 1 to 10 on your fingers.
Just like you, the _decimal_ number system gets its name from the ten digits it
has to work with.
In mathematics, we typically count from 0 to 9 and then increment the next digit
up and start counting over again at 10.
The _decimal_ system is also called _base 10_ for this reason.
```
735 = 7 hundreds + 3 tens + 5 ones
```
We could also write this as:
```
735 = 7 * 10^2 + 2 * 10^1 + 5 * 10^0
```
Lots of other popular counting systems exist.
_Sexagesimal_ uses 60 digits (0..59). It is the basis for timekeeping and
measuring angles.
## Binary numbers
If we want to count with just two digits (0 and 1), we have to use a _base 2_
system called _binary_.
```
101 = 1 * 2^2 + 0 * 2^1 + 1 * 2^0
```
101 in binary is equal to 5 in decimal.
_Bits_ is shorthand for _binary digits_, or every digit in a binary number.
---
# Binary data
With binary way we can use computers to do numeric calculations on integers and
fractions. There are some limitations however.
## Integers
In mathematics, integers count upwards and downwards forever. But computers have
limited space to organize binary numbers. For example, a 32-bit computer uses
32-bit _words_ to represent numbers.
This only allows you to represent numbers as big as `2^32`, or a little more
than 4 billion.
## Fractions (Real numbers)
Representing real numbers in binary is hard. No matter how we do it, it's always
going to be an approximation of the mathematically understood truth.
```rust
fn main() {
println!("{}", 1.1 + 2.2);
}
```
---
# Binary data
There are some units worth knowing when working with binary data. You already
know about _bits_, or _binary digits_. This is the smallest unit of measure, as
a _bit_ is just a 0 or a 1.
## Bytes
_Bytes_ are chunks of 8 bits. Storage, for example, is typically calculated in
_bytes_ e.g., 8GB of RAM is about 8 billion _bytes_.
## Words
Computer _words_ are made up of _bytes_. A 32-bit word is made up of 4 bytes
(`32 = 4 * 8`).
---
# Representing text
Since everything in a computer is stored as binary numbers, we need a way of
mapping those numbers into letters so we can work with text.
## Characters
Characters are letters, printed digits, punctuation, whitespace, and unprintable
controls used to write human-readable text on a computer.
## Printable characters
```
a, b, c
1, 2, 3
. " ; )
```
## Unprintable characters
- newline (`\n`)
- tab (`\t`)
- carriage return (`\r`)
## Standards
The first standard set, ASCII, was designed in 1963. Every character had an
associated number:
```bash
man -P "head -25" ascii
```
---
# Representing text
## Unicode
Today we use Unicode, or an implementation of Unicode called UTF-8 for almost
all web pages and modern programming languages. It is a superset of ASCII with
support for thousands of multilingual characters, mathematical symbols, and
shapes.
UTF-8 is the default for Rust and what we'll be using.
```rust
fn main() {
println!("Parlez-vous français?");
}
```
---
# Representing images and sound
If you look closely you can see the screen you're looking at now is made up of
many tiny squares. When combined and viewed at a distance, all of these picture
elements make up an image.
These picture elements, or _pixels_, consist of location coordinates and color.
We can use different color _schemes_ and image _resolutions_ to create, store,
and display different images.
Sound on the other hand, is normally described as a wave. This wave equates to
levels of air pressure our ears can detect to register that sound. We can
approximate a wave (take a sample) with a series of heights at given intervals.
To record sound, we can use microphones and digital sampling to build a wave
approximation in the computer. To play sound back, we reproduce the wave by
playing the sample height at the rate it was recorded. This wave is pushed out
through the speakers and registers as the same sound we heard when we recorded
it before.
|