Rust,Hello World

资料
Rust 程序设计语言 简体中文版
The Rust Programming Language

1. Rust安装

2. vscode配置

参考如何使用VSCode配置Rust开发环境(VS Code 安装 Rust 常用插件) 博客,配置了下面5个插件

  • rust-analyzer
  • crates
  • Rust Syntax
  • Even Better TOML
  • Rust Test Lens

3. Hello World

  • 新建第一个rust项目 hello_world

  • 在项目内新建main.rs文件,并将以下代码复制进去

    
    fn main() {
        println!("Hello, world!");
    }
    
  • 编译 rustc main.rs

  • 执行 ./main.rs

    从以上步骤可知

    • main是程序执行的入口,没有入参也没有返回值
    • 编译和运行是独立的

4. 使用 cargo 管理rust项目

  • 使用 cargo 创建项目

    cargo new hello_cargo
    

    执行完该命令后会生成以下文件结构

    -hello_cargo
      src
        main.rs
      .gitignore
      Cargo.toml
    
  • 构建并运行Cargo项目

    • 方式一:cargo build & ./target/debug/hello_cargo
      这个命令会创建一个可执行文件 target/debug/hello_cargo

    • 方式二:cargo run

  • 其他cargo命令

    • cargo check:在不生成二进制文件的情况下构建项目来检查错误
    • cargo build --release:优化编译项目(可以让 Rust 代码运行的更快,需要消耗更长的编译时间),为用户构建最终程序
posted @ 2023-08-17 22:57  想成长的菜鸟  阅读(79)  评论(0)    收藏  举报