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://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,33 @@ 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 has more than one file, we would need to
100
+ call ` rustc ` more than once and pass it a bunch of options to tell it to build
101
+ everything together. With Cargo, as our project grows, we can just `cargo
102
+ build`, and it’ll work the right way.
103
+
104
+ When your project is finally ready for release, you can use
105
+ ` cargo build --release ` to compile your project with optimizations.
93
106
94
107
You'll also notice that Cargo has created a new file: ` Cargo.lock ` .
95
108
@@ -100,27 +113,34 @@ version = "0.0.1"
100
113
```
101
114
102
115
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
116
+ Right now, we don’ t have any, so it’ s a bit sparse. You won't ever need
104
117
to touch this file yourself, just let Cargo handle it.
105
118
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.
119
+ That’s it! We’ve successfully built ` hello_world ` with Cargo. Even though our
120
+ program is simple, it’s using much of the real tooling that you’ll use for the
121
+ rest of your Rust career. You can expect to do this to get started with
122
+ virtually all Rust projects:
123
+
124
+ ``` bash
125
+ $ git clone someurl.com/foo
126
+ $ cd foo
127
+ $ cargo build
128
+ ```
109
129
110
130
## A New Project
111
131
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.
132
+ You don’ t have to go through this whole process every time you want to start a
133
+ new project! Cargo has the ability to make a bare-bones project directory in
134
+ which you can start developing right away.
115
135
116
136
To start a new project with Cargo, use ` cargo new ` :
117
137
118
138
``` bash
119
139
$ cargo new hello_world --bin
120
140
```
121
141
122
- We' re passing ` --bin ` because we're making a binary program: if we
123
- were making a library, we'd leave it off.
142
+ We’ re passing ` --bin ` because we're making a binary program: if we were making
143
+ a library, we'd leave it off.
124
144
125
145
Let's check out what Cargo has generated for us:
126
146
@@ -135,10 +155,10 @@ $ tree .
135
155
1 directory, 2 files
136
156
```
137
157
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.
158
+ If you don't have the ` tree ` command, you can probably get it from your
159
+ distribution’s package manager. It’ s not necessary, but it’ s certainly useful.
140
160
141
- This is all we need to get started. First, let' s check out ` Cargo.toml ` :
161
+ This is all we need to get started. First, let’ s check out ` Cargo.toml ` :
142
162
143
163
``` toml
144
164
[package ]
@@ -148,21 +168,32 @@ version = "0.0.1"
148
168
authors = [
" Your Name <[email protected] >" ]
149
169
```
150
170
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.
171
+ Cargo has populated this file with reasonable defaults based off the arguments
172
+ you gave it and your ` git ` global configuration. You may notice that Cargo has
173
+ also initialized the ` hello_world ` directory as a ` git ` repository.
154
174
155
- Here' s what' s in ` src/main.rs ` :
175
+ Here’ s what’ s in ` src/main.rs ` :
156
176
157
177
``` rust
158
178
fn main () {
159
179
println! (" Hello, world!" );
160
180
}
161
181
```
162
182
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 ) .
183
+ Cargo has generated a "Hello World!" for us, and you’ re ready to start coding! Cargo
184
+ has its own [ guide] [ guide ] which covers Cargo’s features in much more depth .
165
185
166
- Now that you've got the tools down, let's actually learn more about the Rust
186
+ [ guide ] : http://doc.crates.io/guide.html
187
+
188
+ Now that you’ve got the tools down, let’s actually learn more about the Rust
167
189
language itself. These are the basics that will serve you well through the rest
168
190
of your time with Rust.
191
+
192
+ You have two options: Dive into a project with ‘[ Learn Rust] [ learnrust ] ’, or
193
+ start from the bottom and work your way up with ‘[ Syntax and
194
+ Semantics] [ synatax ] ’. More experienced systems programmers will probably prefer
195
+ ‘Learn Rust’, while those from dynamic backgrounds may enjoy either. Different
196
+ people learn differently! Choose whatever’s right for you.
197
+
198
+ [ learnrust ] : learn-rust.html
199
+ [ syntax ] : syntax-and-semantics.html
0 commit comments