rust学习笔记-2

变量:默认是不可修改的 let用于声明变量,修改变量会导致编译会报错 fn main() { let x = 5; println!("The value of x is: {x}"); x = 6; println!("The value of x is: {x}"); } 加mut声明x为可修改的: fn main() { let mut x = 5; println!("The value of x is: {x}"); x = 6; println!("The value of x is: {x}"); } 话说rust的编译报错的错误提示还是蛮好看的: % cargo build Compiling variables v0.1.0 error[E0384]: cannot assign twice to immutable variable `x` --> src/main.rs:4:5 | 2 | let x = 5; | - | | | first assignment to `x` | help: consider making this binding mutable: `mut x` 3 | println!...

June 8, 2024 · akawhy

rust学习笔记-1

基于官网文档,做下笔记 安装 # Installing rustup on Linux or macOS curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh 直接按回车按default模式安装。安装完后会多不少命令: rust-analyzer rust-gdb rust-gdbgui rust-lldb rustc rustdoc rustfmt rustup cargo cargo-clippy cargo-fmt cargo-miri 更新rust版本的话可以使用 rustup update 编译运行程序 使用rustc编译代码生成可执行文件,执行即可运行。对于稍微大型的项目可以使用cargo进行安装依赖、编译、运行。使用cargo创建项目后的目录结构 % tree # tree命令在macos下需要用homebrew安装下 . ├── Cargo.toml └── src └── main.rs 运行万cargo build和cargo run之后的目录。展示下2层(因为生成的文件比较多) % tree -L 2 . ├── Cargo.lock ├── Cargo.toml ├── src │ └── main.rs └── target ├── CACHEDIR.TAG └── debug 可以在项目中的Cargo.toml中指定依赖。使用cargo build会自动安装。如果感觉下载的慢的话,推荐替换为阿里云的crates源。操作方法: 拷贝如下信息至~/....

June 7, 2024 · akawhy