Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
a548e818
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a548e818
编写于
6月 25, 2014
作者:
S
Steve Klabnik
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Guide: Hello, cargo
上级
b20f968e
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
113 addition
and
0 deletion
+113
-0
src/doc/guide.md
src/doc/guide.md
+113
-0
未找到文件。
src/doc/guide.md
浏览文件 @
a548e818
...
@@ -264,26 +264,139 @@ projects.
...
@@ -264,26 +264,139 @@ projects.
## Hello, Cargo!
## Hello, Cargo!
[
Cargo
](
http://crates.io
)
is a tool that Rustaceans use to help manage their
Rust projects. Cargo is currently in an alpha state, just like Rust, and so it
is still a work in progress. However, it is already good enough to use for many
Rust projects, and so it is assumed that Rust projects will use Cargo from the
beginning.
Programmers love car analogies, so I've got a good one for you to think about
the relationship between
`cargo`
and
`rustc`
:
`rustc`
is like a car, and
`cargo`
is like a robotic driver. You can drive your car yourself, of course,
but isn't it just easier to let a computer drive it for you?
Anyway, Cargo manages three things: building your code, downloading the
dependencies your code needs, and building the dependencies your code needs.
At first, your program doesn't have any dependencies, so we'll only be using
the first part of its functionality. Eventually, we'll add more. Since we
started off by using Cargo, it'll be easy to add later.
Let's convert Hello World to Cargo. The first thing we need to do is install
it. To do this, we need to build it from source. There are no binaries yet.
First, let's go back to our projects directory. We don't want Cargo to
live in our project!
```
{bash}
$ cd ..
```
Next, we need these commands:
```
{bash}
$ git clone --recursive https://github.com/rust-lang/cargo
$ cd cargo
$ make
$ make install # may need sudo or admin permissions
```
The
`--recursive`
downloads Cargo's own dependencies. You can't use Cargo to
fetch dependencies until you have Cargo installed!
Let's see if that worked. Try this:
```
{bash}
$ cargo
Commands:
build # compile the current project
Options (for all commands):
-v, [--verbose]
-h, [--help]
```
If you see this output when you run
`cargo`
, congrats! Cargo is working. If
not, please
[
open an Issue
](
https://github.com/rust-lang/cargo/issues/new
)
or
drop by the Rust IRC, and we can help you out.
Let's move back into our
`hello_world`
directory now:
```
{bash}
$ cd .. # move back up into projects
$ cd hello_world # move into hello_world
```
To Cargo-ify our project, we need to do two things: Make a
`Cargo.toml`
configuration file, and put our source file in the right place. Let's
do that part first:
```
{bash}
$ mkdir src
$ mv hello_world.rs src/hello_world.rs
```
Cargo expects your source files to live inside a
`src`
directory. That leaves
the top level for other things, like READMEs, licence information, and anything
not related to your code. Cargo helps us keep our projects nice and tidy. A
place for everything, and everything in its place.
Next, our configuration file:
```
{bash}
$ editor Cargo.toml
```
Make sure to get this name right: you need the capital
`C`
!
Put this inside:
```
[package]
name = "hello_world"
version = "0.1.0"
authors = [ "someone@example.com" ]
[[bin]]
name = "hello_world"
```
This file is in the
[
TOML
](
https://github.com/toml-lang/toml
)
format. Let's let
it explain itself to you:
> TOML aims to be a minimal configuration file format that's easy to read due
> to obvious semantics. TOML is designed to map unambiguously to a hash table.
> TOML should be easy to parse into data structures in a wide variety of
> languages.
TOML is very similar to INI, but with some extra goodies.
Anyway, there are two
**table**
s in this file:
`package`
and
`bin`
. The first
tells Cargo metadata about your package. The second tells Cargo that we're
interested in building a binary, not a library (though we could do both!), as
well as what it is named.
Once you have this file in place, we should be ready to build! Try this:
```
{bash}
$ cargo build
Compiling hello_world v0.1.0 (file:/home/yourname/projects/hello_world)
$ ./target/hello_world
Hello, world!
```
Bam! We build our project with
`cargo build`
, and run it with
`./target/hello_world`
. This hasn't bought us a whole lot over our simple use
of
`rustc`
, but think about the future: when our project has more tha one file,
we would need to call
`rustc`
twice, and pass it a bunch of options to tell it
to build everything together. With Cargo, as our project grows, we can just
`cargo build`
and it'll work the right way.
That's it! We've successfully built
`hello_world`
with Cargo. Even though our
program is simple, it's using all of the real tooling that you'll use for the
rest of your Rust career.
Next, we'll learn more about Rust itself, by starting to write a more complicated
program. We hope you want to do more with Rust than just print "Hello, world!"
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录