-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.html
373 lines (274 loc) · 8.94 KB
/
index.html
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
<DOCTYPE html>
<html>
<head>
<title>a very brief intro to rust</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link href="public/skeleton.css" type="text/css" rel="stylesheet">
<link href="public/custom.css" type="text/css" rel="stylesheet">
</head>
<body>
<textarea id="source">
class: middle, center

# a very brief intro to rust
---
class: middle, left
## links
- web: https://ashleygwilliams.github.io/a-very-brief-intro-to-rust/#1
- github: https://github.com/ashleygwilliams/a-very-brief-intro-to-rust
### PRs and issues welcome!
made by [@ag_dubs](https://twitter.com/ag_dubs) who is, at best, an advanced beginner in Rust
---
class: middle, left
### This guide is an intro to Rust syntax. It doesn't touch on concepts at all.
### Concepts are more important, but sometimes you need a little boost to get to a point where the concepts make sense.
### This is that boost.
---
class: middle, left
## install rust
The best way to install Rust is with [rustup](https://www.rustup.rs/). rustup is a Rust
version manager. To install it type:
```
curl https://sh.rustup.rs -sSf | sh
```
To keep your rust up-to-date with the latest stable version of rust,
type:
```
rustup update
```
To check which version of Rust you have type:
```
rustc --version
```
---
class: middle, left
## setting up a project
There are many ways to setup a project in Rust, but this is the simplest.
1. Create a repository on Github
2. Clone the repository
3. `cd` into the repository directory
4. Type `cargo init .`
* Use `--bin` if you are not writing a library
This will create several files and folders for you automatically:
- `Cargo.toml`: metadata about your project and its dependencies
- `.gitignore`: ignores compiled files built by Rust
- `src/lib.rs` or `src/main.rs`: where your Rust code goes
---
class: middle, left
## setting up a project
#### `lib.rs` vs `main.rs`
There are 2 main types of projects you can make in Rust: a library and not
a library.
If you are writing a <strong>library</strong>, it means you intend for your
code to be used in someone else's application as a crate or module.
If you want to do this, you should use a `lib.rs`.
If you are writing a <strong>not library</strong>, it means that you'd like
to write code that compiles into a binary that someone can run. If you
want to do this, you need to use a `main.rs`. Inside the `main.rs` you
should have a `main` function that looks like this:
```rust
fn main() {
// your app code goes here
}
```
---
class: middle, left
## cargo
Cargo is a tool that helps you develop Rust. It does several things:
- Runs tasks: `cargo build` (compile your app), `cargo test` (test your app), `cargo run` (run your app)
- Start a project: `cargo new`, `cargo init`
Cargo is also the package manager for Rust. This means that you can
use Cargo to install and manage bits of other people's code.
- A package in Rust is called a Crate.
- You can find Crates on http://crates.io
- You list the Crates you want to use in the `Cargo.toml` file
- Your app keeps track of what crates you are using in the `Cargo.lock` file
---
class: middle, left
## storing values
To get started using Rust you'll probably want to assign values so that
you can use them. To do this in Rust:
```
let name = "ashley";
let age = 30;
```
If you want to make a constant, you must specify a type:
```
const FAVENUM: u32 = 6;
```
---
class: middle, left
## types
There are a lot of types, but just to get you started:
- `u32`: unsigned 32-bit integer
- `i32`: signed 32-bit integer
- `String` and/or `&str`: more on these below
- `bool`: a boolean
---
class: middle, left
## dealing with strings
Strings in Rust are a lot more complicated than you might be used to if
you are coming from another language, in particular, interpreted languages
like Ruby or JavaScript. Here's some key points:
#### `&str` and `String`
- "my string" is not a `String`. It's a `str`. the difference between a `String` and a
`str` is how they are allocated. Don't worry about that right now.
- Pretty much always use `str` with an `&`, as `&str`.
- You can turn a `&str` into a `String` by using `to_string()` or `String::from()`. You want
to do this because `String` has a ton of awesome convenience methods.
---
class: middle, left
## concatenation
- add a `&str` to a `String` using `push_str()`
```rust
let mut realstring = String::from("hello ");
let str1 = "world!";
realstring.push_str(str1);
```
- add `&str`s using `format!`
```rust
let str1 = "hello ";
let str2 = "world!";
let message = format!("{}{}", str1, str2);
```
---
class: middle, left
## characters
- a `char` is a different type than a `str` or `String`. `char` always uses single quotes.
- to get a `String`'s `char`s you can call `chars()`
- you might find that instead of `chars()` you really want `as_bytes()` but if you aren't sure
don't sweat it right now.
Example:
```rust
let name = String::from("ashley");
let letters = name.chars();
for l in letters {
// do something cool with characters
}
```
---
class: middle, left
## macros
Macros are an interesting part of Rust. You know something is a macro if its name has
a `!`. The least technical way to describe the cool thing about macros is that they
kinda get compiled twice. Don't worry if that doesn't make any sense.
- `println!` is the equivalent of `console.log` or `puts`. It prints printable things
to standard output, which is usually just the console.
```rust
println!("i get printed on the screen");
println!("hello {}!", "world");
```
- `format!` is also a macro. We talked about it before as a way to
concatenate `str`s.
```rust
format!("my dogs are named: {} and {}", "cheeto", "frito");
```
---
class: middle, left
## function signatures
```rust
pub fn say_hello(name: &str) -> String {
let message = format!("hello, {}!", name);
message
}
```
- put `pub` at the beginning if you want the function to be accessible outside the file
as in a module or crate
- the keyword `fn` is how we know it is a function
- list parameters inside the parens in the style `parameter_name: Type`, separate by commas
- use the `->` to say what type the function returns
- return a value from the last line of a function by omitting the semicolon
- return early in a function using the `return` keyword
---
class: middle, left
## `match` syntax
Rust has pattern matching and it's great!
```rust
match animal {
"cat" => "Meow",
"dog" => "Woof",
_ => "<indecipherable>", // trailing comma!
}
```
- `_` is used as a catch-all for anything that doesn't match
- `match` supports trailing commas, and it's best practice to use them :)
---
class: middle, left
## the `Option` type
Rust doesn't have `nil`/`null` so if you want to express that something might return
something or nothing, you need to use the `Option` type.
To write the `Option` type, write the word `Option`, followed by angle brackets with
a Type inside, e.g. `Option<u32>`.
For example, if a parameter is optional you'd write:
```rust
fn greeting(name: Option<&str>) -> String {
let who = match name {
Some(n) => n,
None => "World",
};
format!("Hello, {}!", who)
}
greeting(Some("ashley"));
// "Hello, ashley!"
greeting(None);
// "Hello, World!"
```
---
class: middle, left
## the `Result` type
`Result` is kind of like `Option` except instead of something or nothing, you
expect something that is Ok (`Ok()`) or an error (`Err()`).
To write the `Result` type, write the word `Result`, follow by angle brackets with
a Type and an Error Type inside, e.g. `Result<u32, &'static str>`.
For example:
```rust
fn parse_name(name: Option<&str>) -> Result<&str, &'static str> {
match name {
Some(n) => Ok(n),
None => Err("You must provide a name."),
}
}
```
---
class: middle, left
## testing
Rust has unit testing built in! You can write tests in their own file or right
inline with your code.
To designate a test write `#[test]` above a code block with asserts:
```rust
fn say_hello(name: &str) -> String {
let who = match name {
Some(n) => n,
None => "World",
};
format!("Hello, {}!", who)
}
#[test]
fn it_should_say_hello() {
assert_eq!(say_hello(None), String::from("Hello, World!"));
assert_eq!(say_hello(Some("World")), String::from("Hello, World!"));
assert_eq!(say_hello(Some("ashley")), String::from(("Hello, ashley!"));
}
```
---
class: middle, left
## good resources
- The Rust Docs, https://doc.rust-lang.org/
- The Rust Book, https://doc.rust-lang.org/book/
- into_rust Screencasts, http://intorust.com/
- The Rust Play Ground, https://play.rust-lang.org/
- Rust by Example, http://rustbyexample.com/
- Rust on exercism.io, http://exercism.io/languages/rust
- New Rustacean Podcast, http://www.newrustacean.com/
---
class: middle, center

# go forth and write some Rust!
</textarea>
<script src="public/remark.js" type="text/javascript">
</script>
<script type="text/javascript">
var slideshow = remark.create();
</script>
</body>
</html>