1
1
% Hello, Cargo!
2
2
3
- [ Cargo] ( http://crates.io ) is a tool that Rustaceans use to help manage their
4
- Rust projects. Cargo is currently in a pre-1.0 state, just like Rust, and so it
5
- is still a work in progress. However, it is already good enough to use for many
6
- Rust projects, and so it is assumed that Rust projects will use Cargo from the
7
- beginning.
3
+ [ Cargo] [ cratesio ] is a tool that Rustaceans use to help manage their Rust
4
+ projects. Cargo is currently in a pre-1.0 state, and so it is still a work in
5
+ progress. However, it is already good enough to use for many Rust projects, and
6
+ so it is assumed that Rust projects will use Cargo from the beginning.
7
+
8
+ [ cratesio ] : https://doc.crates.io
8
9
9
10
Cargo manages three things: building your code, downloading the dependencies
10
11
your code needs, and building those dependencies. At first, your
11
- program doesn' t have any dependencies, so we' ll only be using the first part of
12
- its functionality. Eventually, we' ll add more. Since we started off by using
12
+ program doesn’ t have any dependencies, so we’ ll only be using the first part of
13
+ its functionality. Eventually, we’ ll add more. Since we started off by using
13
14
Cargo, it'll be easy to add later.
14
15
15
- If you installed Rust via the official installers you will also have
16
- Cargo. If you installed Rust some other way, you may want to [ check
17
- the Cargo
18
- README ] ( https://github.com/rust-lang/cargo#installing-cargo-from-nightlies )
19
- for specific instructions about installing it.
16
+ If you installed Rust via the official installers you will also have Cargo. If
17
+ you installed Rust some other way, you may want to [ check the Cargo
18
+ README ] [ cargoreadme ] for specific instructions about installing it.
19
+
20
+ [ cargoreadme ] : https://github.com/rust-lang/cargo#installing-cargo-from-nightlies
20
21
21
22
## Converting to Cargo
22
23
23
- Let' s convert Hello World to Cargo.
24
+ Let’ s convert Hello World to Cargo.
24
25
25
26
To Cargo-ify our project, we need to do two things: Make a ` Cargo.toml `
26
27
configuration file, and put our source file in the right place. Let's
@@ -52,14 +53,9 @@ Put this inside:
52
53
name = " hello_world"
53
54
version = " 0.0.1"
54
55
authors = [
" Your name <[email protected] >" ]
55
-
56
- [[bin ]]
57
-
58
- name = " hello_world"
59
56
```
60
57
61
- This file is in the [ TOML] ( https://github.com/toml-lang/toml ) format. Let's let
62
- it explain itself to you:
58
+ This file is in the [ TOML] [ toml ] format. Let’s let it explain itself to you:
63
59
64
60
> TOML aims to be a minimal configuration file format that's easy to read due
65
61
> to obvious semantics. TOML is designed to map unambiguously to a hash table.
@@ -68,10 +64,7 @@ it explain itself to you:
68
64
69
65
TOML is very similar to INI, but with some extra goodies.
70
66
71
- Anyway, there are two * tables* in this file: ` package ` and ` bin ` . The first
72
- tells Cargo metadata about your package. The second tells Cargo that we're
73
- interested in building a binary, not a library (though we could do both!), as
74
- well as what it is named.
67
+ [ toml ] : https://github.com/toml-lang/toml
75
68
76
69
Once you have this file in place, we should be ready to build! Try this:
77
70
@@ -83,13 +76,32 @@ Hello, world!
83
76
```
84
77
85
78
Bam! We build our project with ` cargo build ` , and run it with
86
- ` ./target/debug/hello_world ` . This hasn't bought us a whole lot over our simple use
87
- of ` rustc ` , but think about the future: when our project has more than one
88
- file, we would need to call ` rustc ` more than once and pass it a bunch of options to
89
- tell it to build everything together. With Cargo, as our project grows, we can
90
- just ` cargo build ` , and it'll work the right way. When your project is finally
91
- ready for release, you can use ` cargo build --release ` to compile your crates with
92
- optimizations.
79
+ ` ./target/debug/hello_world ` . We can do both in one step with ` cargo run ` :
80
+
81
+ ``` bash
82
+ $ cargo run
83
+ Running ` target/debug/hello_world`
84
+ Hello, world!
85
+ ```
86
+
87
+ Notice that we didn’t re-build the project this time. Cargo figured out that
88
+ we hadn’t changed the source file, and so it just ran the binary. If we had
89
+ made a modification, we would have seen it do both:
90
+
91
+ ``` bash
92
+ $ cargo build
93
+ Compiling hello_world v0.0.1 (file:///home/yourname/projects/hello_world)
94
+ Running ` target/debug/hello_world`
95
+ Hello, world!
96
+ ```
97
+
98
+ This hasn’t bought us a whole lot over our simple use of ` rustc ` , but think
99
+ about the future: when our project gets more complex, we would need to do more
100
+ things to get all of the parts to properly compile. With Cargo, as our project
101
+ grows, we can just ` cargo build ` , and it’ll work the right way.
102
+
103
+ When your project is finally ready for release, you can use
104
+ ` cargo build --release ` to compile your project with optimizations.
93
105
94
106
You'll also notice that Cargo has created a new file: ` Cargo.lock ` .
95
107
@@ -100,27 +112,34 @@ version = "0.0.1"
100
112
```
101
113
102
114
This file is used by Cargo to keep track of dependencies in your application.
103
- Right now, we don' t have any, so it' s a bit sparse. You won't ever need
115
+ Right now, we don’ t have any, so it’ s a bit sparse. You won't ever need
104
116
to touch this file yourself, just let Cargo handle it.
105
117
106
- That's it! We've successfully built ` hello_world ` with Cargo. Even though our
107
- program is simple, it's using much of the real tooling that you'll use for the
108
- rest of your Rust career.
118
+ That’s it! We’ve successfully built ` hello_world ` with Cargo. Even though our
119
+ program is simple, it’s using much of the real tooling that you’ll use for the
120
+ rest of your Rust career. You can expect to do this to get started with
121
+ virtually all Rust projects:
122
+
123
+ ``` bash
124
+ $ git clone someurl.com/foo
125
+ $ cd foo
126
+ $ cargo build
127
+ ```
109
128
110
129
## A New Project
111
130
112
- You don' t have to go through this whole process every time you want to start a new
113
- project! Cargo has the ability to make a bare-bones project directory in which you
114
- can start developing right away.
131
+ You don’ t have to go through this whole process every time you want to start a
132
+ new project! Cargo has the ability to make a bare-bones project directory in
133
+ which you can start developing right away.
115
134
116
135
To start a new project with Cargo, use ` cargo new ` :
117
136
118
137
``` bash
119
138
$ cargo new hello_world --bin
120
139
```
121
140
122
- We' re passing ` --bin ` because we're making a binary program: if we
123
- were making a library, we'd leave it off.
141
+ We’ re passing ` --bin ` because we're making a binary program: if we were making
142
+ a library, we'd leave it off.
124
143
125
144
Let's check out what Cargo has generated for us:
126
145
@@ -135,10 +154,10 @@ $ tree .
135
154
1 directory, 2 files
136
155
```
137
156
138
- If you don't have the ` tree ` command, you can probably get it from your distro's package
139
- manager. It' s not necessary, but it' s certainly useful.
157
+ If you don't have the ` tree ` command, you can probably get it from your
158
+ distribution’s package manager. It’ s not necessary, but it’ s certainly useful.
140
159
141
- This is all we need to get started. First, let' s check out ` Cargo.toml ` :
160
+ This is all we need to get started. First, let’ s check out ` Cargo.toml ` :
142
161
143
162
``` toml
144
163
[package ]
@@ -148,21 +167,32 @@ version = "0.0.1"
148
167
authors = [
" Your Name <[email protected] >" ]
149
168
```
150
169
151
- Cargo has populated this file with reasonable defaults based off the arguments you gave
152
- it and your ` git ` global configuration. You may notice that Cargo has also initialized
153
- the ` hello_world ` directory as a ` git ` repository.
170
+ Cargo has populated this file with reasonable defaults based off the arguments
171
+ you gave it and your ` git ` global configuration. You may notice that Cargo has
172
+ also initialized the ` hello_world ` directory as a ` git ` repository.
154
173
155
- Here' s what' s in ` src/main.rs ` :
174
+ Here’ s what’ s in ` src/main.rs ` :
156
175
157
176
``` rust
158
177
fn main () {
159
178
println! (" Hello, world!" );
160
179
}
161
180
```
162
181
163
- Cargo has generated a "Hello World!" for us, and you' re ready to start coding! A
164
- much more in-depth guide to Cargo can be found [ here ] ( http://doc.crates.io/guide.html ) .
182
+ Cargo has generated a "Hello World!" for us, and you’ re ready to start coding! Cargo
183
+ has its own [ guide] [ guide ] which covers Cargo’s features in much more depth .
165
184
166
- Now that you've got the tools down, let's actually learn more about the Rust
185
+ [ guide ] : http://doc.crates.io/guide.html
186
+
187
+ Now that you’ve got the tools down, let’s actually learn more about the Rust
167
188
language itself. These are the basics that will serve you well through the rest
168
189
of your time with Rust.
190
+
191
+ You have two options: Dive into a project with ‘[ Learn Rust] [ learnrust ] ’, or
192
+ start from the bottom and work your way up with ‘[ Syntax and
193
+ Semantics] [ syntax ] ’. More experienced systems programmers will probably prefer
194
+ ‘Learn Rust’, while those from dynamic backgrounds may enjoy either. Different
195
+ people learn differently! Choose whatever’s right for you.
196
+
197
+ [ learnrust ] : learn-rust.html
198
+ [ syntax ] : syntax-and-semantics.html
0 commit comments