mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
format code in md
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
1.
|
||||
|
||||
```rust
|
||||
struct Container(i32, i32);
|
||||
|
||||
@ -53,6 +54,7 @@ fn main() {
|
||||
```
|
||||
|
||||
2.
|
||||
|
||||
```rust
|
||||
impl<T: Sub<Output = T>> Sub<Point<T>> for Point<T> {
|
||||
type Output = Self;
|
||||
@ -93,6 +95,7 @@ impl<T: Sub<Output = T>> Sub for Point<T> {
|
||||
```
|
||||
|
||||
3.
|
||||
|
||||
```rust
|
||||
trait Pilot {
|
||||
fn fly(&self) -> String;
|
||||
@ -134,6 +137,7 @@ fn main() {
|
||||
```
|
||||
|
||||
4.
|
||||
|
||||
```rust
|
||||
trait Person {
|
||||
fn name(&self) -> String;
|
||||
@ -209,6 +213,7 @@ fn main() {
|
||||
```
|
||||
|
||||
5.
|
||||
|
||||
```rust
|
||||
use std::fmt;
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
1.
|
||||
|
||||
```rust
|
||||
struct Array<T, const N: usize> {
|
||||
data : [T; N]
|
||||
@ -20,6 +21,7 @@ fn main() {
|
||||
```
|
||||
|
||||
2.
|
||||
|
||||
```rust
|
||||
fn print_array<T: std::fmt::Debug, const N: usize>(arr: [T; N]) {
|
||||
println!("{:?}", arr);
|
||||
@ -33,7 +35,8 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
3.
|
||||
3.
|
||||
|
||||
```rust
|
||||
#![allow(incomplete_features)]
|
||||
#![feature(generic_const_exprs)]
|
||||
|
@ -1,4 +1,5 @@
|
||||
1.
|
||||
|
||||
```rust
|
||||
struct A; // Concrete type `A`.
|
||||
struct S(A); // Concrete type `S`.
|
||||
@ -27,6 +28,7 @@ fn main() {
|
||||
```
|
||||
|
||||
2.
|
||||
|
||||
```rust
|
||||
fn sum<T:std::ops::Add<Output = T>>(x: T, y: T) -> T {
|
||||
x + y
|
||||
@ -39,8 +41,8 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
3.
|
||||
|
||||
```rust
|
||||
struct Point<T> {
|
||||
x: T,
|
||||
@ -54,6 +56,7 @@ fn main() {
|
||||
```
|
||||
|
||||
4.
|
||||
|
||||
```rust
|
||||
// modify this struct to make the code work
|
||||
struct Point<T, U> {
|
||||
@ -68,6 +71,7 @@ fn main() {
|
||||
```
|
||||
|
||||
5.
|
||||
|
||||
```rust
|
||||
struct Val<T> {
|
||||
val: T,
|
||||
@ -88,6 +92,7 @@ fn main() {
|
||||
```
|
||||
|
||||
6.
|
||||
|
||||
```rust
|
||||
struct Point<T, U> {
|
||||
x: T,
|
||||
@ -115,6 +120,7 @@ fn main() {
|
||||
```
|
||||
|
||||
7.
|
||||
|
||||
```rust
|
||||
struct Point<T> {
|
||||
x: T,
|
||||
|
@ -1,4 +1,5 @@
|
||||
1.
|
||||
|
||||
```rust
|
||||
trait Bird {
|
||||
fn quack(&self) -> String;
|
||||
@ -58,6 +59,7 @@ fn hatch_a_bird(species: u8) ->Box<dyn Bird> {
|
||||
```
|
||||
|
||||
2.
|
||||
|
||||
```rust
|
||||
trait Bird {
|
||||
fn quack(&self);
|
||||
@ -101,6 +103,7 @@ fn main() {
|
||||
```
|
||||
|
||||
3.
|
||||
|
||||
```rust
|
||||
trait Draw {
|
||||
fn draw(&self) -> String;
|
||||
@ -139,6 +142,7 @@ fn draw_with_ref(x: &dyn Draw) {
|
||||
```
|
||||
|
||||
4.
|
||||
|
||||
```rust
|
||||
trait Foo {
|
||||
fn method(&self) -> String;
|
||||
@ -174,6 +178,7 @@ fn main() {
|
||||
```
|
||||
|
||||
5.
|
||||
|
||||
```rust
|
||||
trait MyTrait {
|
||||
fn f(&self) -> Self;
|
||||
|
@ -1,4 +1,5 @@
|
||||
1.
|
||||
|
||||
```rust
|
||||
trait Hello {
|
||||
fn say_hi(&self) -> String {
|
||||
@ -37,6 +38,7 @@ fn main() {
|
||||
```
|
||||
|
||||
2.
|
||||
|
||||
```rust
|
||||
// `Centimeters`, a tuple struct that can be compared
|
||||
#[derive(PartialEq, PartialOrd)]
|
||||
@ -84,6 +86,7 @@ fn main() {
|
||||
```
|
||||
|
||||
3.
|
||||
|
||||
```rust
|
||||
use std::ops;
|
||||
|
||||
@ -101,6 +104,7 @@ fn main() {
|
||||
```
|
||||
|
||||
4.
|
||||
|
||||
```rust
|
||||
use std::ops;
|
||||
|
||||
@ -141,6 +145,7 @@ fn main() {
|
||||
```
|
||||
|
||||
5.
|
||||
|
||||
```rust
|
||||
// implement `fn summary` to make the code work
|
||||
// fix the errors without removing any code line
|
||||
@ -197,6 +202,7 @@ fn summary(t: &impl Summary) {
|
||||
```
|
||||
|
||||
6.
|
||||
|
||||
```rust
|
||||
struct Sheep {}
|
||||
struct Cow {}
|
||||
@ -272,6 +278,7 @@ fn main() {
|
||||
```
|
||||
|
||||
7.
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
assert_eq!(sum(1, 2), 3);
|
||||
@ -298,6 +305,7 @@ where
|
||||
```
|
||||
|
||||
8.
|
||||
|
||||
```rust
|
||||
struct Pair<T> {
|
||||
x: T,
|
||||
@ -337,6 +345,7 @@ fn main() {
|
||||
```
|
||||
|
||||
9.
|
||||
|
||||
```rust
|
||||
fn example1() {
|
||||
// `T: Trait` is the commonly used way
|
||||
|
Reference in New Issue
Block a user