You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-17 22:57:35 +02:00
Trim off licenses when showing solutions (#1212)
The licenses end up in the PO files, causing extra unnecessary for work our translators. We save about 300 lines from each PO file with this. This also solves another small problem: when a file is included with an anchor, other anchors are automatically stripped away. This removes some confusing `// ANCHOR: foo` and `// ANCHOR_END: foo` lines in the solutions.
This commit is contained in:
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: solution
|
||||||
// ANCHOR: top
|
// ANCHOR: top
|
||||||
#![no_main]
|
#![no_main]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: solution
|
||||||
// ANCHOR: top
|
// ANCHOR: top
|
||||||
#![no_main]
|
#![no_main]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: solution
|
||||||
use core::ptr::{addr_of, addr_of_mut};
|
use core::ptr::{addr_of, addr_of_mut};
|
||||||
|
|
||||||
#[repr(C, align(4))]
|
#[repr(C, align(4))]
|
||||||
|
@ -7,11 +7,11 @@
|
|||||||
`main.rs`:
|
`main.rs`:
|
||||||
|
|
||||||
```rust,compile_fail
|
```rust,compile_fail
|
||||||
{{#include rtc/src/main.rs}}
|
{{#include rtc/src/main.rs:solution}}
|
||||||
```
|
```
|
||||||
|
|
||||||
`pl031.rs`:
|
`pl031.rs`:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
{{#include rtc/src/pl031.rs}}
|
{{#include rtc/src/pl031.rs:solution}}
|
||||||
```
|
```
|
||||||
|
@ -5,5 +5,5 @@
|
|||||||
([back to exercise](compass.md))
|
([back to exercise](compass.md))
|
||||||
|
|
||||||
```rust,compile_fail
|
```rust,compile_fail
|
||||||
{{#include compass/src/main.rs}}
|
{{#include compass/src/main.rs:solution}}
|
||||||
```
|
```
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: solution
|
||||||
// ANCHOR: setup
|
// ANCHOR: setup
|
||||||
use futures_util::stream::StreamExt;
|
use futures_util::stream::StreamExt;
|
||||||
use futures_util::SinkExt;
|
use futures_util::SinkExt;
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: solution
|
||||||
// ANCHOR: setup
|
// ANCHOR: setup
|
||||||
use futures_util::sink::SinkExt;
|
use futures_util::sink::SinkExt;
|
||||||
use futures_util::stream::StreamExt;
|
use futures_util::stream::StreamExt;
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: solution
|
||||||
// ANCHOR: Philosopher
|
// ANCHOR: Philosopher
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::time;
|
use tokio::time;
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: solution
|
||||||
// ANCHOR: Philosopher
|
// ANCHOR: Philosopher
|
||||||
use std::sync::{mpsc, Arc, Mutex};
|
use std::sync::{mpsc, Arc, Mutex};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: solution
|
||||||
use std::{sync::Arc, sync::Mutex, sync::mpsc, thread};
|
use std::{sync::Arc, sync::Mutex, sync::mpsc, thread};
|
||||||
|
|
||||||
// ANCHOR: setup
|
// ANCHOR: setup
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
([back to exercise](dining-philosophers-async.md))
|
([back to exercise](dining-philosophers-async.md))
|
||||||
|
|
||||||
```rust,compile_fail
|
```rust,compile_fail
|
||||||
{{#include dining-philosophers-async.rs}}
|
{{#include dining-philosophers-async.rs:solution}}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Broadcast Chat Application
|
## Broadcast Chat Application
|
||||||
@ -15,12 +15,12 @@
|
|||||||
`src/bin/server.rs`:
|
`src/bin/server.rs`:
|
||||||
|
|
||||||
```rust,compile_fail
|
```rust,compile_fail
|
||||||
{{#include chat-async/src/bin/server.rs}}
|
{{#include chat-async/src/bin/server.rs:solution}}
|
||||||
```
|
```
|
||||||
|
|
||||||
`src/bin/client.rs`:
|
`src/bin/client.rs`:
|
||||||
|
|
||||||
```rust,compile_fail
|
```rust,compile_fail
|
||||||
{{#include chat-async/src/bin/client.rs}}
|
{{#include chat-async/src/bin/client.rs:solution}}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
([back to exercise](dining-philosophers.md))
|
([back to exercise](dining-philosophers.md))
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
{{#include dining-philosophers.rs}}
|
{{#include dining-philosophers.rs:solution}}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Link Checker
|
## Link Checker
|
||||||
@ -13,5 +13,5 @@
|
|||||||
([back to exercise](link-checker.md))
|
([back to exercise](link-checker.md))
|
||||||
|
|
||||||
```rust,compile_fail
|
```rust,compile_fail
|
||||||
{{#include link-checker.rs}}
|
{{#include link-checker.rs:solution}}
|
||||||
```
|
```
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: solution
|
||||||
// ANCHOR: transpose
|
// ANCHOR: transpose
|
||||||
fn transpose(matrix: [[i32; 3]; 3]) -> [[i32; 3]; 3] {
|
fn transpose(matrix: [[i32; 3]; 3]) -> [[i32; 3]; 3] {
|
||||||
// ANCHOR_END: transpose
|
// ANCHOR_END: transpose
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: solution
|
||||||
// ANCHOR: luhn
|
// ANCHOR: luhn
|
||||||
pub fn luhn(cc_number: &str) -> bool {
|
pub fn luhn(cc_number: &str) -> bool {
|
||||||
// ANCHOR_END: luhn
|
// ANCHOR_END: luhn
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
([back to exercise](luhn.md))
|
([back to exercise](luhn.md))
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
{{#include luhn.rs}}
|
{{#include luhn.rs:solution}}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Pattern matching
|
## Pattern matching
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
([back to exercise](for-loops.md))
|
([back to exercise](for-loops.md))
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
{{#include for-loops.rs}}
|
{{#include for-loops.rs:solution}}
|
||||||
```
|
```
|
||||||
### Bonus question
|
### Bonus question
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: solution
|
||||||
// ANCHOR: setup
|
// ANCHOR: setup
|
||||||
struct Library {
|
struct Library {
|
||||||
books: Vec<Book>,
|
books: Vec<Book>,
|
||||||
|
@ -5,5 +5,5 @@
|
|||||||
([back to exercise](strings-iterators.md))
|
([back to exercise](strings-iterators.md))
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
{{#include strings-iterators.rs}}
|
{{#include strings-iterators.rs:solution}}
|
||||||
```
|
```
|
||||||
|
@ -5,5 +5,5 @@
|
|||||||
([back to exercise](book-library.md))
|
([back to exercise](book-library.md))
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
{{#include book-library.rs}}
|
{{#include book-library.rs:solution}}
|
||||||
```
|
```
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: solution
|
||||||
// ANCHOR: prefix_matches
|
// ANCHOR: prefix_matches
|
||||||
pub fn prefix_matches(prefix: &str, request_path: &str) -> bool {
|
pub fn prefix_matches(prefix: &str, request_path: &str) -> bool {
|
||||||
// ANCHOR_END: prefix_matches
|
// ANCHOR_END: prefix_matches
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: solution
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||||
// ANCHOR: Point
|
// ANCHOR: Point
|
||||||
pub struct Point {
|
pub struct Point {
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: solution
|
||||||
// ANCHOR: ffi
|
// ANCHOR: ffi
|
||||||
mod ffi {
|
mod ffi {
|
||||||
use std::os::raw::{c_char, c_int};
|
use std::os::raw::{c_char, c_int};
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: solution
|
||||||
// ANCHOR: setup
|
// ANCHOR: setup
|
||||||
pub trait Widget {
|
pub trait Widget {
|
||||||
/// Natural width of `self`.
|
/// Natural width of `self`.
|
||||||
|
@ -5,5 +5,5 @@
|
|||||||
([back to exercise](safe-ffi-wrapper.md))
|
([back to exercise](safe-ffi-wrapper.md))
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
{{#include safe-ffi-wrapper.rs}}
|
{{#include safe-ffi-wrapper.rs:solution}}
|
||||||
```
|
```
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
([back to exercise](simple-gui.md))
|
([back to exercise](simple-gui.md))
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
{{#include simple-gui.rs}}
|
{{#include simple-gui.rs:solution}}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Points and Polygons
|
## Points and Polygons
|
||||||
@ -13,5 +13,5 @@
|
|||||||
([back to exercise](points-polygons.md))
|
([back to exercise](points-polygons.md))
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
{{#include points-polygons.rs}}
|
{{#include points-polygons.rs:solution}}
|
||||||
```
|
```
|
||||||
|
@ -5,8 +5,3 @@ You will find solutions to the exercises on the following pages.
|
|||||||
Feel free to ask questions about the solutions [on
|
Feel free to ask questions about the solutions [on
|
||||||
GitHub](https://github.com/google/comprehensive-rust/discussions). Let us know
|
GitHub](https://github.com/google/comprehensive-rust/discussions). Let us know
|
||||||
if you have a different or better solution than what is presented here.
|
if you have a different or better solution than what is presented here.
|
||||||
|
|
||||||
|
|
||||||
> **Note:** Please ignore the `// ANCHOR: label` and `// ANCHOR_END: label`
|
|
||||||
> comments you see in the solutions. They are there to make it possible to
|
|
||||||
> re-use parts of the solutions as the exercises.
|
|
||||||
|
Reference in New Issue
Block a user