注释
注释是给程序员看的,而不是给电脑看的。写注释是为了帮助别人理解你的代码。 这也有利于帮助你以后理解你的代码。 (很多人写了很好的代码,但后来却忘记了他们为什么要写它。)在Rust中写注释,你通常使用 //
.
fn main() { // Rust programs start with fn main() // You put the code inside a block. It starts with { and ends with } let some_number = 100; // We can write as much as we want here and the compiler won't look at it }
当你这样做时,编译器不会看//
右边的任何东西。
还有一种注释,你用/*
开始写,*/
结束写。这个写在你的代码中间很有用。
fn main() { let some_number/*: i16*/ = 100; }
对编译器来说,let some_number/*: i16*/ = 100;
看起来像let some_number = 100;
。
/* */
形式对于超过一行的非常长的注释也很有用。在这个例子中,你可以看到你需要为每一行写//
。但是如果您输入 /*
,它不会停止,直到您用 */
完成它。
fn main() { let some_number = 100; /* Let me tell you a little about this number. It's 100, which is my favourite number. It's called some_number but actually I think that... */ let some_number = 100; // Let me tell you // a little about this number. // It's 100, which is my favourite number. // It's called some_number but actually I think that... }