YouTip LogoYouTip

Cargo Tutorial

In Rust development, almost all projects are managed and built using Cargo, because it provides a convenient workflow and powerful features, making Rust development more efficient and reliable. ### What is Cargo Cargo is Rust's official build system and package manager. It has two main purposes: It has two main purposes: * **Project Management**: Cargo is used to create, build, and manage Rust projects. With Cargo, you can easily create new projects, manage project dependencies, and perform operations such as building, running, and testing projects. * **Package Manager**: Cargo also serves as Rust's package manager. It allows developers to introduce and manage dependencies (such as third-party libraries) in their projects, and ensures version management and compatibility of these dependencies. **Cargo Main Features and Functions:** * **Dependency Management**: Cargo manages project dependencies through the `Cargo.toml` file, which lists all external libraries required by the project and their versions. * **Build System**: Cargo uses the Rust compiler (rustc) to build projects, automatically handling dependency compilation and linking. * **Package Registry**: Cargo interacts with [crates.io](https://crates.io/), the Rust community's package registry, allowing developers to search for, add, and manage third-party libraries. * **Build Configuration**: Through `Cargo.toml` and `Cargo.lock` files, Cargo allows developers to configure build options such as compiler options, features, and target platforms. * **Project Templates**: Cargo provides templates for creating new projects, allowing you to quickly start a new project using the `cargo new` command. * **Testing**: Cargo provides a simple command `cargo test` to run unit tests for the project. * **Benchmarking**: Cargo supports benchmarking using the `cargo bench` command. * **Publishing**: Through the `cargo publish` command, developers can publish their libraries to crates.io for other developers to use. * **Custom Build Scripts**: Cargo allows the use of custom build scripts to handle more complex build requirements. * **Multi-target Projects**: Cargo supports defining multiple targets in a single project, such as executables, libraries, tests, and benchmarks. * **Cross-platform Building**: Cargo supports building Rust programs across multiple platforms, including Windows, macOS, Linux, and various embedded systems. * **Build Cache**: To speed up builds, Cargo uses build cache to store compiled dependencies. * **Offline Work**: Cargo supports working without an internet connection; it automatically uses locally cached dependencies. * **Plugin System**: Cargo allows developers to write plugins to extend its functionality. * **Environment Variables**: Cargo supports overriding default build and run behaviors through environment variables. ### Cargo Functions In addition to creating projects, Cargo also has functions such as building and running projects. Building and running correspond to the following commands: * `cargo new `: Create a new Rust project. * `cargo build`: Compile the current project. * `cargo run`: Compile and run the current project. * `cargo check`: Check for syntax and type errors in the current project. * `cargo test`: Run unit tests for the current project. * `cargo update`: Update dependencies specified in Cargo.toml to the latest versions. * `cargo --help`: View Cargo's help information. * `cargo publish`: Publish the Rust project to crates.io. * `cargo clean`: Clean up temporary files and directories generated during the build process. ### Configuring Rust Project in VSCode Cargo is a great build tool, and if you combine VSCode with it, VSCode will be a very convenient development environment. In the previous chapter, we created the greeting project. Now let's open the greeting folder with VSCode (**note: not tutorial-greeting**). After opening greeting, create a new folder named .vscode inside it (note the dot before vscode; if this folder already exists, you don't need to create it). Create two files named tasks.json and launch.json in the newly created .vscode folder. The file contents are as follows: ## tasks.json File {"version": "2.0.0", "tasks": [{"label": "build", "type": "shell", "command":"cargo", "args": }]} ## launch.json File (For Windows) {"version": "0.2.0", "configurations": [{"name": "(Windows) Launch", "preLaunchTask": "build", "type": "cppvsdbg", "request": "launch", "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false}, {"name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "Fill in the directory where GDB is located", "setupCommands": [{"description": "Enable pretty printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true}]}]} ## launch.json File (For Linux) {"version": "0.2.0", "configurations": [{"name": "Debug", "type": "gdb", "preLaunchTask":"build", "request": "launch", "target": "${workspaceFolder}/target/debug/${workspaceFolderBasename}", "cwd":"${workspaceFolder}"}]} ## launch.json File (For Mac OS) {"version": "0.2.0", "configurations": [{"name": "(lldb) Launch", "type": "cppdbg", "preLaunchTask": "build", "request": "launch", "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "lldb"}]} Then click "Run" in the left sidebar of VSCode. If you are using MSVC, select "(Windows) Launch". If you are using MinGW and have GDB installed, select "(gdb) Launch". Before launching with gdb, make sure to fill
← Rust Basic SyntaxRust Tutorial β†’