Running cargo build
will succeed for the following code:
#[cfg(test)]
mod tests {
#[test]
fn func() {
let x = 1;
sss
}
}
but will fail for this code:
#[cfg(test)]
mod tests {
#[test]
fn func() {
sss
let x = 1;
}
}
error: expected `;`, found keyword `let`
--> src/lib.rs:5:12
|
5 | sss
| ^ help: add `;` here
6 | let x = 1;
| --- unexpected token
A section of the Rust Book on Test Organization says:
The
#[cfg(test)]
annotation on the tests module tells Rust to compile and run the test code only when you runcargo test
, not when you runcargo build
.
So why does Rust still compile mod tests
that is annotated with #[cfg(test)]
?