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://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,33 @@ 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 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.
93106
94107You'll also notice that Cargo has created a new file: ` Cargo.lock ` .
95108
@@ -100,27 +113,34 @@ version = "0.0.1"
100113```
101114
102115This 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
104117to touch this file yourself, just let Cargo handle it.
105118
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+ ```
109129
110130## A New Project
111131
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.
115135
116136To start a new project with Cargo, use ` cargo new ` :
117137
118138``` bash
119139$ cargo new hello_world --bin
120140```
121141
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.
124144
125145Let's check out what Cargo has generated for us:
126146
@@ -135,10 +155,10 @@ $ tree .
1351551 directory, 2 files
136156```
137157
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.
140160
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 ` :
142162
143163``` toml
144164[package ]
@@ -148,21 +168,32 @@ version = "0.0.1"
148168authors = [
" Your Name <[email protected] >" ]
149169```
150170
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.
154174
155- Here' s what' s in ` src/main.rs ` :
175+ Here’ s what’ s in ` src/main.rs ` :
156176
157177``` rust
158178fn main () {
159179 println! (" Hello, world!" );
160180}
161181```
162182
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 .
165185
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
167189language itself. These are the basics that will serve you well through the rest
168190of 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