sudo dnf install rust-toolset
rustc -Vcargo -V
sudo dnf install rustuprustup toolchain install stable
cargo -V to verify the installation result. The version number, commit hash, and commit date of the latest stable release are displayed.rustup toolchain install stable again:rustup toolchain install stableinfo: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'stable-x86_64-unknown-linux-gnu unchanged - rustc 1.69.0 (84c898d65 2023-04-16)error: rustup is not installed at '/root/.cargo'
cargo new command, for example, creating a classic project:cargo new hello_world --bin # In earlier versions of cargo, you must specify --bin to create a binary project.cargo new hello_world # Same as above.
cargo new --lib hello_lib
bin and lib. The former is a runnable project, and the latter is a dependency library project.cargo, you had to add the --bin parameter when creating a project. The cargo version provided here creates a bin project by default, so you do not need to explicitly specify the --bin parameter.tree.├── Cargo.toml└── src└── main.rs
Cargo.toml: a project description file or manifest specific to Cargo, which contains all the metadata required for Cargo to compile the project.Cargo.lock: the Cargo.lock file is a detailed list of project dependencies generated by the Cargo tool based on the toml file of the same project, and generally does not need to be modified.src: the directory where source code is stored.src/main.rs: the main executable file. When editing project code, modify main.rs and add new source files to the src subdirectory.benches: the directory for storing benchmarks.examples: the directory for storing sample code.tests: the directory for storing integration test code.Cargo.toml file is interpreted as follows:[package]: defines the metadata of the project (package).name: the project name.version: the project version.authors: the project authors.edition: the major Rust version currently in use.rust-version: the minimum required Rust version.description: the project description.documentation: the documentation.readme: the README file.homepage: the homepage.repository: the source code repository.license: the open source license.License-file: the License file.keywords: the project keywords.categories: the project categories.workspace: the workspace.build: the script build path.links: the name of the local link library.exclude: exclude files during publishing.include: include files during publishing.publish: prevents the project from being published.metadata: additional configuration information for external tools.default-run: the default executable file (binary) used by [cargo run].autobins: disables automatic discovery of executable files.autoexamples: disables automatic discovery of example files.autotests: disables automatic discovery of test files.autobenches: disables automatic discovery of bench files.resolver: sets the dependency resolver.
cargo build
# View the file typefile target/debug/hello_worldtarget/debug/hello_world: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=c87cf5694fab5ba052821001f3d9c31070543413, for GNU/Linux 3.2.0, with debug_info, not stripped# Run Manually./target/debug/hello_world
cargo runCompiling hello_world v0.1.0 (/root/test/hello_world)Finished dev [unoptimized + debuginfo] target(s) in 0.25sRunning `target/debug/hello_world`
cargo run first compiles the project and then runs it, so it is effectively equivalent to running two separate commands, which simplifies the workflow for users.debug mode is run by default.tree -L 2.├── Cargo.lock├── Cargo.toml├── src│ └── main.rs└── target├── CACHEDIR.TAG└── debug # debug mode
debug mode, code compiles quickly, allowing developers to build smoothly. However, this also means slower runtime performance, which can negatively impact release. Therefore, you can explicitly specify the release mode when publishing:cargo run --releasecargo build --release
release directory will be created under target:tree -L 2.├── Cargo.lock├── Cargo.toml├── src│ └── main.rs└── target├── CACHEDIR.TAG├── debug└── release # release mode
cargo run and cargo build to verify code correctness can be time-consuming. In this case, you can use cargo check to quickly compile the code:cargo checkFinished dev [unoptimized + debuginfo] target(s) in 0.02s
() or Result<(), E> where E: Error.cargo test # debug modecargo test --release # release mode
///;cargo doc --no-deps
rustfmt is a tool for formatting Rust code that helps developers ensure code cleanliness, consistency, and readability. You can use rustfmt to automatically format Rust code according to the community-recommended code style, providing consistent code standards. rustfmt can be used either independently or together with Cargo.rustfmt, ensure that rustfmt is installed. If it is not installed yet, you can easily install it by running the following command:dnf install rustfmt
rustfmt, you can use it as follows:rustfmt command, the program formats the specified Rust source file according to the default configuration specifications. By default, rustfmt outputs the formatted content directly to the terminal.rustfmt src/test.rs
rustfmt to directly modify the source file by formatting it in place, you can use the --emit flag:rustfmt --emit files src/test.rs
rustfmt xx.rs can format a specified file, while the cargo fmt command automatically applies rustfmt formatting rules to format all .rs files in the project.cargo fmt
rustfmt.toml in the project root directory or any parent directory. In this file, you can add key-value pair configurations without quotation marks, for example:max_width = 100reorder_imports = true
rustfmt.toml file, rustfmt automatically applies the configuration options in it to format your code.Cargo.toml is modified, validation fails.error: the listed checksum of `/builddir/build/BUILD/gnome-tour-43.0/vendor/glib-macros/Cargo.toml` has changed:expected: d6e10d3ec19f19b5a32cdcd159567ede9fa1e59ac13f446f0a1e40f99e7e6c1bactual: ea00aa6587f4f1793c296fd4a18c9989239e13df703c9254906b1c757714d440directory sources are not intended to be edited, if modifications are required then it is recommended that `[patch]` is used with a forked copy of the source
.cargo-checksum.json accordingly. To resolve it, find the Cargo.toml entry in the json file and change it to the actual value. The same applies to checksum errors for other files.# Change "Cargo.toml" to "d6e10d3ec19f19b5a32cdcd159567ede9fa1e59ac13f446f0a1e40f99e7e6c1b"# Change it to "Cargo.toml":"ea00aa6587f4f1793c296fd4a18c9989239e13df703c9254906b1c757714d440"
Cargo.toml describes the version requirements, while Cargo.lock is automatically generated based on the build environment.Cargo.toml file in gnome-tour-43.0, you can see that the build requires gtk3 version 0.4.x.[dependencies]gtk = { package = "gtk3", version = "0.4", features= ["v4_2"]}log = "0.4"gettext-rs = { version = "0.7", features = ["gettext-system"] }adw = {package = "libadwaita", version = "0.1"}pretty_env_logger = "0.4"regex = "1.5"
Cargo.lock is first generated, the build source provides gtk3 version 0.4.8, so Cargo.lock locks the version to 0.4.8.
When it is used later, the source provides gtk3 upgraded to 0.4.9, and the build fails with an error:error: failed to select a version for the requirement `gtk3 = "^0.4"` (locked to 0.4.8)candidate versions found which didn't match: 0.4.9
Cargo.toml in gnome-tour-43.0, you can see that gtk3-0.4.9 already meets the build requirements. Therefore, this error is not necessary, and you need to update the Cargo.lock file to eliminate it. Since Cargo.lock is automatically generated from Cargo.toml, manual modification is generally not recommended. Instead, you should trigger the update by adjusting Cargo.toml.vendor directory of gnome-tour-43.0 contains a gtk3 folder, update version in vendor/gtk3/Cargo.toml to 0.4.9.[package]edition = "2021"rust-version = "1.56"name = "gtk3"version = "0.4.9"
cargo update in the gnome-tour-43.0 directory to automatically update the Cargo.lock file:cargo updateUpdating gtk3 v0.4.8 -> v0.4.9
フィードバック