1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-07-07 06:55:42 +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:
Dustin J. Mitchell
2025-02-18 15:13:16 -05:00
committed by GitHub
parent 699c5137c7
commit 44a79741ff
38 changed files with 138 additions and 144 deletions

View File

@ -26,15 +26,10 @@ transpose a matrix (turn rows into columns):
Copy the code below to <https://play.rust-lang.org/> and implement the function.
This function only operates on 3x3 matrices.
```rust,should_panic
// TODO: remove this when you're done with your implementation.
#![allow(unused_variables, dead_code)]
```rust,should_panic,editable
{{#include exercise.rs:transpose}}
todo!()
}
{{#include exercise.rs:tests}}
{{#include exercise.rs:main}}
```

View File

@ -25,7 +25,23 @@ fn transpose(matrix: [[i32; 3]; 3]) -> [[i32; 3]; 3] {
result
}
// ANCHOR: tests
// ANCHOR: main
fn main() {
let matrix = [
[101, 102, 103], // <-- the comment makes rustfmt add a newline
[201, 202, 203],
[301, 302, 303],
];
println!("matrix: {:#?}", matrix);
let transposed = transpose(matrix);
println!("transposed: {:#?}", transposed);
}
// ANCHOR_END: main
// ANCHOR_END: solution
// This test does not appear in the exercise, as this is very early in the course, but it verifies
// that the solution is correct.
#[test]
fn test_transpose() {
let matrix = [
@ -43,18 +59,3 @@ fn test_transpose() {
]
);
}
// ANCHOR_END: tests
// ANCHOR: main
fn main() {
let matrix = [
[101, 102, 103], // <-- the comment makes rustfmt add a newline
[201, 202, 203],
[301, 302, 303],
];
println!("matrix: {:#?}", matrix);
let transposed = transpose(matrix);
println!("transposed: {:#?}", transposed);
}
// ANCHOR_END: main