11% Hello, Cargo!
22
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
89
910Cargo manages three things: building your code, downloading the dependencies
1011your 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
1314Cargo, it'll be easy to add later.
1415
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
2021
2122## Converting to Cargo
2223
23- Let' s convert Hello World to Cargo.
24+ Let’ s convert Hello World to Cargo.
2425
2526To Cargo-ify our project, we need to do two things: Make a ` Cargo.toml `
2627configuration file, and put our source file in the right place. Let's
@@ -52,14 +53,9 @@ Put this inside:
5253name = " hello_world"
5354version = " 0.0.1"
5455authors = [
" Your name <[email protected] >" ]
55-
56- [[bin ]]
57-
58- name = " hello_world"
5956```
6057
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:
6359
6460> TOML aims to be a minimal configuration file format that's easy to read due
6561> to obvious semantics. TOML is designed to map unambiguously to a hash table.
@@ -68,10 +64,7 @@ it explain itself to you:
6864
6965TOML is very similar to INI, but with some extra goodies.
7066
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
7568
7669Once you have this file in place, we should be ready to build! Try this:
7770
@@ -83,13 +76,32 @@ Hello, world!
8376```
8477
8578Bam! 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.
93105
94106You'll also notice that Cargo has created a new file: ` Cargo.lock ` .
95107
@@ -100,27 +112,34 @@ version = "0.0.1"
100112```
101113
102114This 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
104116to touch this file yourself, just let Cargo handle it.
105117
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+ ```
109128
110129## A New Project
111130
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.
115134
116135To start a new project with Cargo, use ` cargo new ` :
117136
118137``` bash
119138$ cargo new hello_world --bin
120139```
121140
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.
124143
125144Let's check out what Cargo has generated for us:
126145
@@ -135,10 +154,10 @@ $ tree .
1351541 directory, 2 files
136155```
137156
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.
140159
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 ` :
142161
143162``` toml
144163[package ]
@@ -148,21 +167,32 @@ version = "0.0.1"
148167authors = [
" Your Name <[email protected] >" ]
149168```
150169
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.
154173
155- Here' s what' s in ` src/main.rs ` :
174+ Here’ s what’ s in ` src/main.rs ` :
156175
157176``` rust
158177fn main () {
159178 println! (" Hello, world!" );
160179}
161180```
162181
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 .
165184
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
167188language itself. These are the basics that will serve you well through the rest
168189of 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