feat: added cleanscript
This commit is contained in:
121
appendix/appendix-04-useful-development-tools.html
Normal file
121
appendix/appendix-04-useful-development-tools.html
Normal file
@@ -0,0 +1,121 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>D - Useful Development Tools</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2 id="appendix-d-useful-development-tools"><a class="header" href="#appendix-d-useful-development-tools">Appendix D: Useful Development Tools</a></h2>
|
||||
<p>In this appendix, we talk about some useful development tools that the Rust
|
||||
project provides. We’ll look at automatic formatting, quick ways to apply
|
||||
warning fixes, a linter, and integrating with IDEs.</p>
|
||||
<h3 id="automatic-formatting-with-rustfmt"><a class="header" href="#automatic-formatting-with-rustfmt">Automatic Formatting with <code>rustfmt</code></a></h3>
|
||||
<p>The <code>rustfmt</code> tool reformats your code according to the community code style.
|
||||
Many collaborative projects use <code>rustfmt</code> to prevent arguments about which
|
||||
style to use when writing Rust: Everyone formats their code using the tool.</p>
|
||||
<p>Rust installations include <code>rustfmt</code> by default, so you should already have the
|
||||
programs <code>rustfmt</code> and <code>cargo-fmt</code> on your system. These two commands are
|
||||
analogous to <code>rustc</code> and <code>cargo</code> in that <code>rustfmt</code> allows finer grained control
|
||||
and <code>cargo-fmt</code> understands conventions of a project that uses Cargo. To format
|
||||
any Cargo project, enter the following:</p>
|
||||
<pre><code class="language-console">$ cargo fmt
|
||||
</code></pre>
|
||||
<p>Running this command reformats all the Rust code in the current crate. This
|
||||
should only change the code style, not the code semantics. For more information
|
||||
on <code>rustfmt</code>, see <a href="https://github.com/rust-lang/rustfmt">its documentation</a>.</p>
|
||||
<h3 id="fix-your-code-with-rustfix"><a class="header" href="#fix-your-code-with-rustfix">Fix Your Code with <code>rustfix</code></a></h3>
|
||||
<p>The <code>rustfix</code> tool is included with Rust installations and can automatically
|
||||
fix compiler warnings that have a clear way to correct the problem that’s
|
||||
likely what you want. You’ve probably seen compiler warnings before. For
|
||||
example, consider this code:</p>
|
||||
<p><span class="filename">Filename: src/main.rs</span></p>
|
||||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||||
let mut x = 42;
|
||||
println!("{x}");
|
||||
}</code></pre>
|
||||
<p>Here, we’re defining the variable <code>x</code> as mutable, but we never actually mutate
|
||||
it. Rust warns us about that:</p>
|
||||
<pre><code class="language-console">$ cargo build
|
||||
Compiling myprogram v0.1.0 (file:///projects/myprogram)
|
||||
warning: variable does not need to be mutable
|
||||
--> src/main.rs:2:9
|
||||
|
|
||||
2 | let mut x = 0;
|
||||
| ----^
|
||||
| |
|
||||
| help: remove this `mut`
|
||||
|
|
||||
= note: `#[warn(unused_mut)]` on by default
|
||||
</code></pre>
|
||||
<p>The warning suggests that we remove the <code>mut</code> keyword. We can automatically
|
||||
apply that suggestion using the <code>rustfix</code> tool by running the command <code>cargo fix</code>:</p>
|
||||
<pre><code class="language-console">$ cargo fix
|
||||
Checking myprogram v0.1.0 (file:///projects/myprogram)
|
||||
Fixing src/main.rs (1 fix)
|
||||
Finished dev [unoptimized + debuginfo] target(s) in 0.59s
|
||||
</code></pre>
|
||||
<p>When we look at <em>src/main.rs</em> again, we’ll see that <code>cargo fix</code> has changed the
|
||||
code:</p>
|
||||
<p><span class="filename">Filename: src/main.rs</span></p>
|
||||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||||
let x = 42;
|
||||
println!("{x}");
|
||||
}</code></pre>
|
||||
<p>The variable <code>x</code> is now immutable, and the warning no longer appears.</p>
|
||||
<p>You can also use the <code>cargo fix</code> command to transition your code between
|
||||
different Rust editions. Editions are covered in <a href="appendix-05-editions.html">Appendix E</a><!--
|
||||
ignore -->.</p>
|
||||
<h3 id="more-lints-with-clippy"><a class="header" href="#more-lints-with-clippy">More Lints with Clippy</a></h3>
|
||||
<p>The Clippy tool is a collection of lints to analyze your code so that you can
|
||||
catch common mistakes and improve your Rust code. Clippy is included with
|
||||
standard Rust installations.</p>
|
||||
<p>To run Clippy’s lints on any Cargo project, enter the following:</p>
|
||||
<pre><code class="language-console">$ cargo clippy
|
||||
</code></pre>
|
||||
<p>For example, say you write a program that uses an approximation of a
|
||||
mathematical constant, such as pi, as this program does:</p>
|
||||
<figure class="listing">
|
||||
<span class="file-name">Filename: src/main.rs</span>
|
||||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||||
let x = 3.1415;
|
||||
let r = 8.0;
|
||||
println!("the area of the circle is {}", x * r * r);
|
||||
}</code></pre>
|
||||
</figure>
|
||||
<p>Running <code>cargo clippy</code> on this project results in this error:</p>
|
||||
<pre><code class="language-text">error: approximate value of `f{32, 64}::consts::PI` found
|
||||
--> src/main.rs:2:13
|
||||
|
|
||||
2 | let x = 3.1415;
|
||||
| ^^^^^^
|
||||
|
|
||||
= note: `#[deny(clippy::approx_constant)]` on by default
|
||||
= help: consider using the constant directly
|
||||
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant
|
||||
</code></pre>
|
||||
<p>This error lets you know that Rust already has a more precise <code>PI</code> constant
|
||||
defined, and that your program would be more correct if you used the constant
|
||||
instead. You would then change your code to use the <code>PI</code> constant.</p>
|
||||
<p>The following code doesn’t result in any errors or warnings from Clippy:</p>
|
||||
<figure class="listing">
|
||||
<span class="file-name">Filename: src/main.rs</span>
|
||||
<pre class="playground"><code class="language-rust edition2024">fn main() {
|
||||
let x = std::f64::consts::PI;
|
||||
let r = 8.0;
|
||||
println!("the area of the circle is {}", x * r * r);
|
||||
}</code></pre>
|
||||
</figure>
|
||||
<p>For more information on Clippy, see <a href="https://github.com/rust-lang/rust-clippy">its documentation</a>.</p>
|
||||
<h3 id="ide-integration-using-rust-analyzer"><a class="header" href="#ide-integration-using-rust-analyzer">IDE Integration Using <code>rust-analyzer</code></a></h3>
|
||||
<p>To help with IDE integration, the Rust community recommends using
|
||||
<a href="https://rust-analyzer.github.io"><code>rust-analyzer</code></a><!-- ignore -->. This tool is a set of
|
||||
compiler-centric utilities that speak <a href="http://langserver.org/">Language Server Protocol</a><!--
|
||||
ignore -->, which is a specification for IDEs and programming languages to
|
||||
communicate with each other. Different clients can use <code>rust-analyzer</code>, such as
|
||||
<a href="https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer">the Rust analyzer plug-in for Visual Studio Code</a>.</p>
|
||||
<p>Visit the <code>rust-analyzer</code> project’s <a href="https://rust-analyzer.github.io">home page</a><!-- ignore -->
|
||||
for installation instructions, then install the language server support in your
|
||||
particular IDE. Your IDE will gain capabilities such as autocompletion, jump to
|
||||
definition, and inline errors.</p>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user