分享好友 编程语言首页 频道列表

Cargo, Rust’s Package Manager

rust文章/教程  2023-02-09 09:190

Installing

The easiest way to get Cargo is to get the current stable release of Rust by using the rustup script:

$ curl -sSf https://static.rust-lang.org/rustup.sh | sh

This will get you the current stable release of Rust for your platform along with the latest Cargo.

If you are on Windows, you can directly download the latest 32bit (Rust and Cargo) or 64bit (Rust andCargo) Rust stable releases or Cargo nightlies.

Alternatively, you can build Cargo from source.

Let’s get started

To start a new project with Cargo, use cargo new:

$ cargo new hello_world --bin

We’re passing --bin because we’re making a binary program: if we were making a library, we’d leave it off.

Let’s check out what Cargo has generated for us:

$ cd hello_world
$ tree .
.
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files

This is all we need to get started. First, let’s check out Cargo.toml:

[package]
name = "hello_world"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]

This is called a manifest, and it contains all of the metadata that Cargo needs to compile your project.

Here’s what’s in src/main.rs:

fn main() {
    println!("Hello, world!");
}

Cargo generated a “hello world” for us. Let’s compile it:

$ cargo build
   Compiling hello_world v0.1.0 (file:///path/to/project/hello_world)

And then run it:

$ ./target/debug/hello_world
Hello, world!

We can also use cargo run to compile and then run it, all in one step:

$ cargo run
     Fresh hello_world v0.1.0 (file:///path/to/project/hello_world)
   Running `target/hello_world`
Hello, world!

Going further

For more details on using Cargo, check out the Cargo Guide

查看更多关于【rust文章/教程】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
Rust到底值不值得学--Rust对比、特色和理念
前言其实我一直弄不明白一点,那就是计算机技术的发展,是让这个世界变得简单了,还是变得更复杂了。当然这只是一个玩笑,可别把这个问题当真。然而对于IT从业者来说,这可不是一个玩笑。几乎每一次的技术发展,都让这个生态变得更为复杂。“英年早秃”已经成

0评论2023-03-08818

全栈程序员的新玩具Rust(三)板条箱
上次用到了stdout,这次我们来写一个更复杂一点的游戏rust的标准库叫做std,默认就会引入。这次我们要用到一个随机数函数,而随机数比较尴尬的一点是这玩意不在标准库中,我们要额外依赖一个库。很多编程方案都有自己的模块化库系统,rust也不例外,不过rust

0评论2023-02-10729

【Rust】标准库-Result rust数据库
环境Rust 1.56.1VSCode 1.61.2概念参考:https://doc.rust-lang.org/stable/rust-by-example/std/result.html示例main.rsmod checked {#[derive(Debug)]pub enum MathError {DivisionByZero,NonPositiveLogarithm,NegativeSquareRoot,}pub type MathResult =

0评论2023-02-09978

【Rust】标准库-引用 rust 数据库框架
环境Rust 1.56.1VSCode 1.61.2概念参考:https://doc.rust-lang.org/stable/rust-by-example/std/rc.html示例rust 使用 Rc 来实现引用计数。main.rsuse std::rc::Rc;fn main() {let rc_examples = "Rc examples".to_string();{println!("--- rc_a is created

0评论2023-02-09638

rust 打印当前时间
let now = time::now();let f_now = time::strftime("%Y-%m-%dT%H:%M:%S", now).unwrap();println!("now: {:?}", f_now);

0评论2023-02-09689

【Rust】线程 rust编程语言
环境Rust 1.56.1VSCode 1.61.2概念参考:https://doc.rust-lang.org/stable/rust-by-example/std_misc/threads.html示例main.rsuse std::thread;const N_THREADS: u32 = 10;fn main() {let mut children = vec![];for i in 0..N_THREADS {children.push(threa

0评论2023-02-09956

更多推荐