You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-15 22:00:26 +02:00
Be more consistent about tests vs. main (#2644)
The content slides all use `fn main`, with the exception of the testing segment. But with this change, where it makes sense exercises use tests instead, and not both tests and `fn main`. A small change in `book.js` supports running tests when a code sample does not have `fn main` but does have `#[test]`, so these work naturally. Fixes #1581.
This commit is contained in:
committed by
GitHub
parent
699c5137c7
commit
44a79741ff
@ -7,6 +7,6 @@ publish = false
|
||||
[lints.rust]
|
||||
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(never)'] }
|
||||
|
||||
[[bin]]
|
||||
[lib]
|
||||
name = "luhn"
|
||||
path = "exercise.rs"
|
||||
|
@ -27,7 +27,7 @@ correctly.
|
||||
Copy the code below to <https://play.rust-lang.org/> and write additional tests
|
||||
to uncover bugs in the provided implementation, fixing any bugs you find.
|
||||
|
||||
```rust
|
||||
```rust,editable
|
||||
{{#include exercise.rs:luhn}}
|
||||
|
||||
{{#include exercise.rs:unit-tests}}
|
||||
|
@ -11,8 +11,8 @@
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#![allow(dead_code)]
|
||||
|
||||
// ANCHOR: solution
|
||||
// This is the buggy version that appears in the problem.
|
||||
#[cfg(never)]
|
||||
// ANCHOR: luhn
|
||||
@ -40,6 +40,7 @@ pub fn luhn(cc_number: &str) -> bool {
|
||||
// ANCHOR_END: luhn
|
||||
|
||||
// This is the solution and passes all of the tests below.
|
||||
// ANCHOR: solution
|
||||
pub fn luhn(cc_number: &str) -> bool {
|
||||
let mut sum = 0;
|
||||
let mut double = false;
|
||||
@ -69,14 +70,6 @@ pub fn luhn(cc_number: &str) -> bool {
|
||||
digits >= 2 && sum % 10 == 0
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cc_number = "1234 5678 1234 5670";
|
||||
println!(
|
||||
"Is {cc_number} a valid credit card number? {}",
|
||||
if luhn(cc_number) { "yes" } else { "no" }
|
||||
);
|
||||
}
|
||||
|
||||
// ANCHOR: unit-tests
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
@ -8,7 +8,7 @@ Rust and Cargo come with a simple unit test framework. Tests are marked with
|
||||
`#[test]`. Unit tests are often put in a nested `tests` module, using
|
||||
`#[cfg(test)]` to conditionally compile them only when building tests.
|
||||
|
||||
```rust,editable,ignore
|
||||
```rust,editable
|
||||
fn first_word(text: &str) -> &str {
|
||||
match text.find(' ') {
|
||||
Some(idx) => &text[..idx],
|
||||
@ -39,9 +39,3 @@ mod tests {
|
||||
|
||||
- This lets you unit test private helpers.
|
||||
- The `#[cfg(test)]` attribute is only active when you run `cargo test`.
|
||||
|
||||
<details>
|
||||
|
||||
Run the tests in the playground in order to show their results.
|
||||
|
||||
</details>
|
||||
|
Reference in New Issue
Block a user