rust 入门

2023/2/11

# Hello, World

main.rs

fn main() {
    println!("hello world!");
}

编译和运行

> rustc main.rs
> .\main.exe
Hello, world!

我们调用了一个被叫作 println! 的宏。假如我们调用的是一个普通函数,那么这里会以去掉!符号的 println 来进行标记。

# Cargo

创建一个 cargo 项目

cargo new hello_cargo

Cargo.toml 配置文件

[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

src/main.rs

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

使用 Cargo 构建项目

cargo build

使用 Cargo 运行项目

cargo run