Rustのコンパイルエラーメッセージ

Rustのコンパイルエラー発生時のエラーメッセージの最初の行にはエラーインデックス番号が含まれている場合がある。 以下の例では[E0596]のように表示されているのがエラーインデックス番号となっている。

error[E0596]: cannot borrow immutable borrowed content `*some_string` as mutable
(エラー: 不変な借用をした中身`*some_string`を可変で借用できません)
 --> error.rs:8:5
  |
7 | fn change(some_string: &String) {
  |                        ------- use `&mut String` here to make mutable
8 |     some_string.push_str(", world");
  |     ^^^^^^^^^^^ cannot borrow as mutable

エラーインデックス番号は公式Webページあるいはrustcコマンドの--explainオプションで内容を確認する事ができる。

$ rustc --explain E0596
This error occurs because you tried to mutably borrow a non-mutable variable.
 
Erroneous code example:
 
let x = 1;
let y = &mut x; // error: cannot borrow mutably
 
In here, x isn't mutable, so when we try to mutably borrow it in y, it fails. To fix this error, you need to make x mutable:
 
let mut x = 1;
let y = &mut x; // ok!