学习rust的基础命令,包括编译,语法错误检查,打包输出。rust学习基本是在 linux 操作,如果windows 注意路径命令用 \ ;Linux命令用 /

1. 新建项目 
(1) 如果已经有了项目目录,进入文件夹后运行

cargo init 

 

# 如果没有对应文件夹会创建,包括 Cargo.toml src/main.rs(二进制) 或者 src/lib.rs 同时初始化 git

(2)直接新建,包括目录创建 Cargo.toml src/main.rs(二进制) 或者 src/lib.rs 同时初始化 git

cargo new my_app

2.运行代码: 默认创建后,在src/main.rs 文件,有个主函数,打印 Hello, world! 如下
代码如下:

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

运行:

# 进入src 文件夹,编译代码
rustc main.rs

# 生成 main 文件,然后运行main 文件 (windows 下是.\main.exe 在 Windows 上一个带有 .pdb 扩展名、包含调试信息的文件)
./main 
Hello, world!

3. rust 入口程序,所有项目都会有一个入口
fn main() {
}


4.打印输出 
println!("打印字符串")
println(函数名)

5. 项目配置以及扩展配置 文件Cargo.toml,Cargo 是运行,管理扩展命令
# 配置如下

[package]
name = "forasp"
version = "0.1.0"
edition = "2024"

[dependencies]
# 编译命令
cargo build          
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.03s

# 生成目标文件 target/debug/forasp 根据项目名称
./target/debug/forasp
Hello, world!

# 编译并生成可执行目标文件,并执行,一步构建并运行项目。
cargo run 
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/forasp`
Hello, world!

# 测试编译, 在不生成二进制文件的情况下构建项目来检查错误,如果有报错会有提示。
cargo check
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s