@@ -17,16 +17,23 @@ lives, but for this book, we'd suggest making a *projects* directory in your
17
17
home directory and keeping all your projects there. Open a terminal and enter
18
18
the following commands to make a directory for this particular project:
19
19
20
+ Linux and Mac:
21
+
20
22
``` bash
21
23
$ mkdir ~ /projects
22
24
$ cd ~ /projects
23
25
$ mkdir hello_world
24
26
$ cd hello_world
25
27
```
26
28
27
- > Note: If you’re on Windows and not using PowerShell, the ` ~ ` that represents
28
- > your home directory may not work.
29
- > Consult the documentation for your shell for more details.
29
+ Windows:
30
+
31
+ ``` bash
32
+ $ mkdir %USERPROFILE%\p rojects
33
+ $ cd %USERPROFILE%\p rojects
34
+ $ mkdir hello_world
35
+ $ cd hello_world
36
+ ```
30
37
31
38
### Writing and Running a Rust Program
32
39
@@ -52,10 +59,10 @@ $ ./main
52
59
Hello, world!
53
60
```
54
61
55
- On Windows, just replace ` main ` with ` main.exe ` . Regardless of your operating
56
- system, you should see the string ` Hello, world! ` print to the terminal. If you
57
- did, then congratulations! You've officially written a Rust program. That makes
58
- you a Rust programmer! Welcome.
62
+ On Windows, just replace ` ./ main` with ` .\ main.exe` . Regardless of your
63
+ operating system, you should see the string ` Hello, world! ` print to the
64
+ terminal. If you did, then congratulations! You've officially written a Rust
65
+ program. That makes you a Rust programmer! Welcome.
59
66
60
67
### Anatomy of a Rust Program
61
68
@@ -129,16 +136,17 @@ main main.rs
129
136
On Windows, you'd enter:
130
137
131
138
``` bash
132
- $ dir
133
- main.exe main.rs
139
+ $ dir /B # the /B option says to only show the file names
140
+ main.exe
141
+ main.rs
134
142
```
135
143
136
144
This shows we have two files: the source code, with the ` .rs ` extension, and the
137
145
executable (` main.exe ` on Windows, ` main ` everywhere else). All that's left to
138
146
do from here is run the ` main ` or ` main.exe ` file, like this:
139
147
140
148
``` bash
141
- $ ./main # or main.exe on Windows
149
+ $ ./main # or .\ main.exe on Windows
142
150
```
143
151
144
152
If * main.rs* were your "Hello, world!" program, this would print `Hello,
@@ -193,11 +201,19 @@ Let's create a new project using Cargo and look at how it differs from our
193
201
project in ` hello_world ` . Go back to your projects directory (or wherever you
194
202
decided to put your code):
195
203
204
+ Linux and Mac:
205
+
196
206
``` bash
197
207
$ cd ~ /projects
198
208
```
199
209
200
- And then run:
210
+ Windows:
211
+
212
+ ``` bash
213
+ $ cd %USERPROFILE%\p rojects
214
+ ```
215
+
216
+ And then on any operating system run:
201
217
202
218
``` bash
203
219
$ cargo new hello_cargo --bin
@@ -213,10 +229,10 @@ directory of the same name that we can then go into.
213
229
If we list the files in the ` hello_cargo ` directory, we can see that Cargo has
214
230
generated two files and one directory for us: a ` Cargo.toml ` and a * src*
215
231
directory with a * main.rs* file inside. It has also initialized a new ` git `
216
- repository in the ` hello_cargo ` directory for us; you can change this to use a
217
- different version control system, or no version control system, by using the
218
- ` --vcs ` flag.
219
-
232
+ repository in the ` hello_cargo ` directory for us, along with a ` .gitignore `
233
+ file; you can change this to use a different version control system, or no
234
+ version control system, by using the ` --vcs ` flag.
235
+ dir
220
236
Open up ` Cargo.toml ` in your text editor of choice. It should look something
221
237
like this:
222
238
@@ -287,10 +303,10 @@ $ cargo build
287
303
```
288
304
289
305
This should have created an executable file in ` target/debug/hello_cargo ` (or
290
- ` target/ debug/ hello_cargo.exe ` on Windows), which you can run with this command:
306
+ ` target\ debug\ hello_cargo.exe ` on Windows), which you can run with this command:
291
307
292
308
``` bash
293
- $ ./target/debug/hello_cargo # or ./ target/ debug/ hello_cargo.exe on Windows
309
+ $ ./target/debug/hello_cargo # or .\ target\ debug\ hello_cargo.exe on Windows
294
310
Hello, world!
295
311
```
296
312
@@ -340,6 +356,10 @@ So a few more differences we've now seen:
340
356
4 . Instead of the result of the build being put in the same directory as our
341
357
code, Cargo will put it in the ` target/debug ` directory.
342
358
359
+ The other advantage of using Cargo is that the commands are the same no matter
360
+ what operating system you're on, so at this point we will no longer be
361
+ providing specific instructions for Linux and Mac versus Windows.
362
+
343
363
### Building for Release
344
364
345
365
When your project is finally ready for release, you can use `cargo build
0 commit comments