跳到主要内容

Rust Hello World

原始方式

  1. 创建 helloworld 文件夹

Linux

mkdir ~/code/rs/
mkdir ~/code/rs/hello_world
cd ~/code/rs/hello_world/

Windows

mkdir D:\code\rs\hello_world
cd /d D:\code\rs\hello_world
  1. 编写源代码

main.rs

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

保存退出后,type 检查内容是否正确。

D:\code\rs\hello_world>type main.rs
fn main() {
println!("Hello, world!");
}
  1. 编译并运行
D:\code\rs\hello_world>rustc main.rs
D:\code\rs\hello_world>main.exe
Hello, world!

cango 方式

$ cd ~/code/rs/
$ cargo new hello_cargo
Created binary (application) `hello_cargo` package
$ cd hello_cargo/
$ cargo build
Compiling hello_cargo v0.1.0 (/home/likun/code/rs/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 0.59s
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/hello_cargo`
Hello, world!
$ cargo check
Checking hello_cargo v0.1.0 (/home/likun/code/rs/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 0.08s
$ cargo build --release
Compiling hello_cargo v0.1.0 (/home/likun/code/rs/hello_cargo)
Finished release [optimized] target(s) in 0.29s