10. อ่าน error ของคอมไพเลอร์

คอมไพเลอร์ของ Rust ไม่ได้ดุ มันเป็นครูที่ละเอียดที่สุดที่คุณจะได้เจอ

อ่าน error อย่างไร

error[E0382]: borrow of moved value: `name`
 --> src/main.rs:4:20
  |
2 |     let name = String::from("ไผ่");
  |         ---- move occurs because `name` has type `String`, which does not implement the `Copy` trait
3 |     let other = name;
  |                 ---- value moved here
4 |     println!("{}", name);
  |                    ^^^^ value borrowed here after move
  |
help: consider cloning the value if the performance cost is acceptable
  |
3 |     let other = name.clone();
  |                     ++++++++
  1. บรรทัดแรก คือปัญหาโดยรวม และรหัส E0382
  2. --> บอกไฟล์ บรรทัด และคอลัมน์ ใน Phai Studio ดับเบิลคลิกในแท็บ Errors เพื่อไปที่นั่น
  3. เส้นใต้ ชี้แต่ละจุดที่เกี่ยวข้อง พร้อมคำอธิบายสั้น ๆ อ่านตามลำดับเลขบรรทัด จะเห็นเรื่องราวว่าเกิดอะไรขึ้น
  4. help: คือวิธีแก้ที่คอมไพเลอร์แนะนำ บ่อยครั้งแก้ตามนี้ได้เลย
ใน Phai Studio กด Ctrl+. ที่เส้นหยัก มักมีปุ่มแก้ให้อัตโนมัติ และถ้าอยากรู้ละเอียด เปิดหน้า https://doc.rust-lang.org/error_codes/E0382.html (เปลี่ยนเลขตามรหัส)

error ที่มือใหม่เจอบ่อย

ข้อความ (ส่วนสำคัญ)แปลว่าแก้อย่างไร
cannot assign twice to immutable variableแก้ค่าตัวแปรที่ไม่ได้ประกาศ mutlet mut x
mismatched types: expected i32, found f64ชนิดไม่ตรงกับที่ต้องการแปลงด้วย as หรือ .parse() หรือแก้ชนิดที่ประกาศไว้
mismatched types: expected i32, found ()ฟังก์ชันควรคืนค่า แต่บรรทัดสุดท้ายมี ;ลบ ; ท้ายบรรทัดที่คืนค่า
borrow of moved valueใช้ตัวแปรหลังจากยกให้คนอื่นไปแล้วส่งเป็น &x แทน หรือ .clone()
cannot borrow `x` as mutable because it is also borrowed as immutableแก้ค่าขณะที่ยังมีคนยืมอ่านอยู่ใช้ค่าที่ยืมให้เสร็จก่อน แล้วค่อยแก้ หรือสลับลำดับ
cannot borrow `x` as mutable, as it is not declared as mutableยืมแก้ตัวแปรที่ไม่มี mutlet mut x
cannot find value `x` in this scopeไม่มีตัวแปรชื่อนี้ตรงนี้ (สะกดผิด หรือประกาศไว้ใน { } อื่น)ตรวจชื่อ และตำแหน่งที่ประกาศ
cannot find type `HashMap` in this scopeยังไม่ได้ useuse std::collections::HashMap; (หรือกด Ctrl+.)
non-exhaustive patternsmatch ไม่ครบทุกกรณีเพิ่มกรณีที่ขาด หรือ _ =>
the `?` operator can only be used in a function that returns `Result`ใช้ ? ในฟังก์ชันที่ไม่ได้คืน Resultเปลี่ยนชนิดที่คืน หรือใช้ match
`Foo` doesn't implement `std::fmt::Display`พิมพ์ด้วย {} ไม่ได้ใช้ {:?} กับ #[derive(Debug)] หรือเขียน impl Display
missing lifetime specifierคืนค่าที่ยืมมา แต่คอมไพเลอร์ไม่รู้ว่ายืมจากอะไรคืนเป็น String แทน &str (ง่ายที่สุด) หรือใส่ lifetime ตาม help

warning ไม่ใช่ error

warning (สีเหลือง) ไม่ทำให้ build ล้มเหลว แต่ควรอ่าน เช่น unused variable (ประกาศแล้วไม่ใช้ ถ้าตั้งใจให้ขึ้นต้นชื่อด้วย _) หรือ unused import และ Build > Run Code Analysis (Clippy) จะแนะนำวิธีเขียนที่ดีขึ้นเพิ่มเติม

เมื่อโปรแกรม panic ตอนรัน

thread 'main' panicked at src/main.rs:12:18:
index out of bounds: the len is 3 but the index is 5

บอกไฟล์และบรรทัดชัดเจน รันด้วย F5 แทน Ctrl+F5 ดีบักเกอร์ของ Phai Studio จะหยุดตรงจุดที่ panic ให้ดูค่าตัวแปร (Locals) และเส้นทางที่มาถึง (Call Stack) ได้ทันที

ทัศนคติที่ช่วยได้