ภาคผนวก D — เครื่องมือพัฒนาที่มีประโยชน์
ในภาคผนวกนี้ เราพูดเกี่ยวกับเครื่องมือพัฒนาที่มีประโยชน์บางอย่างที่ project Rust ให้ เราจะดูที่ formatting อัตโนมัติ, วิธีด่วนในการ apply fix warning, linter และ integrate กับ IDE
Formatting อัตโนมัติด้วย rustfmt
เครื่องมือ rustfmt reformat โค้ดของคุณตาม community code style ผู้
ใช้ร่วมหลาย project ใช้ rustfmt เพื่อป้องกันการโต้แย้งเกี่ยวกับ
style ใดที่ใช้เมื่อเขียน Rust — ทุกคน format โค้ดของพวกเขาโดยใช้
เครื่องมือ
การติดตั้ง Rust รวม rustfmt โดยค่าเริ่มต้น ดังนั้นคุณควรมีโปรแกรม
rustfmt และ cargo-fmt บนระบบของคุณแล้ว สองคำสั่งนี้คล้ายกับ
rustc และ cargo ในที่ rustfmt อนุญาตการควบคุมที่ละเอียดกว่าและ
cargo-fmt เข้าใจ convention ของ project ที่ใช้ Cargo เพื่อ format
project Cargo ใด ใส่ต่อไปนี้:
$ cargo fmt
รันคำสั่งนี้ reformat โค้ด Rust ทั้งหมดใน crate ปัจจุบัน นี่ควรเปลี่ยน
เพียง style โค้ด ไม่ใช่ semantics โค้ด สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ
rustfmt ดู documentation ของมัน
Fix โค้ดของคุณด้วย rustfix
เครื่องมือ rustfix รวมกับการติดตั้ง Rust และ fix warning compiler ที่
มีวิธีชัดเจนในการแก้ปัญหาที่น่าจะเป็นสิ่งที่คุณต้องการได้อัตโนมัติ
คุณน่าจะเห็น warning compiler ก่อน ตัวอย่างเช่น พิจารณาโค้ดนี้:
Filename: src/main.rs
fn main() {
let mut x = 42;
println!("{x}");
}
ที่นี่ เรากำลังนิยามตัวแปร x เป็น mutable แต่เราไม่เคย mutate มันจริง
ๆ Rust เตือนเราเกี่ยวกับนั้น:
$ 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
Warning แนะนำว่าเราลบคีย์เวิร์ด mut เรา apply คำแนะนำนั้นโดยใช้
เครื่องมือ rustfix ได้อัตโนมัติโดยรันคำสั่ง cargo fix:
$ 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
เมื่อเราดูที่ src/main.rs อีก เราจะเห็นว่า cargo fix เปลี่ยนโค้ด:
Filename: src/main.rs
fn main() {
let x = 42;
println!("{x}");
}
ตัวแปร x ตอนนี้ immutable และ warning ไม่ปรากฏอีก
คุณใช้คำสั่ง cargo fix เพื่อ transition โค้ดของคุณระหว่าง edition
Rust ต่างกันได้ด้วย Edition ครอบคลุมใน ภาคผนวก E
Lint เพิ่มด้วย Clippy
เครื่องมือ Clippy คือ collection ของ lint เพื่อวิเคราะห์โค้ดของคุณ เพื่อให้คุณจับความผิดพลาดปกติและปรับปรุงโค้ด Rust ของคุณ Clippy รวม กับการติดตั้ง Rust มาตรฐาน
เพื่อรัน lint ของ Clippy บน project Cargo ใด ใส่ต่อไปนี้:
$ cargo clippy
ตัวอย่างเช่น สมมุติคุณเขียนโปรแกรมที่ใช้ approximation ของ constant mathematical เช่น pi เหมือนโปรแกรมนี้ทำ:
fn main() {
let x = 3.1415;
let r = 8.0;
println!("the area of the circle is {}", x * r * r);
}
รัน cargo clippy บน project นี้ส่งผลเป็น error นี้:
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
error นี้ให้คุณรู้ว่า Rust มี constant PI ที่ precise กว่านิยามแล้ว
และโปรแกรมของคุณจะถูกต้องกว่าถ้าคุณใช้ constant แทน คุณจะแล้วเปลี่ยน
โค้ดของคุณเพื่อใช้ constant PI
โค้ดต่อไปนี้ไม่ส่งผลเป็น error หรือ warning ใดจาก Clippy:
fn main() {
let x = std::f64::consts::PI;
let r = 8.0;
println!("the area of the circle is {}", x * r * r);
}
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ Clippy ดู documentation ของมัน
IDE Integration ใช้ rust-analyzer
เพื่อช่วยกับ IDE integration, Rust community แนะนำให้ใช้
rust-analyzer เครื่องมือนี้คือชุด
ของ utility compiler-centric ที่พูด Language Server Protocol ซึ่งคือ specification สำหรับ IDE และภาษาโปรแกรมในการสื่อสาร
กัน Client ต่างกันใช้ rust-analyzer ได้ เช่น plug-in Rust analyzer
สำหรับ Visual Studio Code
เยี่ยม home page ของ project
rust-analyzer สำหรับคำสั่งติดตั้ง แล้วติดตั้งการสนับสนุน language
server ใน IDE เฉพาะของคุณ IDE ของคุณจะได้ความสามารถเช่น autocompletion,
jump to definition และ inline error