mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
roll back
This commit is contained in:
1
en/.gitignore
vendored
1
en/.gitignore
vendored
@ -1 +0,0 @@
|
||||
book
|
@ -1 +0,0 @@
|
||||
exercise.rs
|
20
en/book.toml
20
en/book.toml
@ -1,20 +0,0 @@
|
||||
[book]
|
||||
title = "Rust Exercise"
|
||||
description = "A set of exercises and examples to pratice various aspects of Rust, also for the rust course (book) at https://course.rs"
|
||||
authors = ["sunface, https://im.dev"]
|
||||
language = "en"
|
||||
|
||||
[output.html.playpen]
|
||||
editable = true
|
||||
editor = "ace"
|
||||
|
||||
[output.html.fold]
|
||||
enable = true
|
||||
|
||||
[output.html]
|
||||
additional-js = ["assets/lang.js"]
|
||||
git-repository-url = "https://github.com/sunface/rust-exercise"
|
||||
edit-url-template = "https://github.com/sunface/rust-exercise/edit/master/{path}"
|
||||
|
||||
[rust]
|
||||
edition = "2021"
|
21
en/deploy
21
en/deploy
@ -1,21 +0,0 @@
|
||||
## this script deploys the static website of course.rs to github pages
|
||||
|
||||
## build static website for book
|
||||
mdbook build
|
||||
## copy CNAME info to book dir
|
||||
cp ./assets/CNAME ./book/
|
||||
cp ./assets/*.html ./book/
|
||||
cp ./assets/sitemap.xml ./book/
|
||||
|
||||
## init git repo
|
||||
cd book
|
||||
git init
|
||||
git config user.name "sunface"
|
||||
git config user.email "cto@188.com"
|
||||
git add .
|
||||
git commit -m 'deploy'
|
||||
git branch -M gh-pages
|
||||
git remote add origin https://github.com/course-rs/rust-exercise
|
||||
|
||||
## push to github pages
|
||||
git push -u -f origin gh-pages
|
@ -1,79 +0,0 @@
|
||||
# Summary
|
||||
|
||||
- [Why Exercise.rs](why-exercise.md)
|
||||
- [Variables](variables.md)
|
||||
- [Basic Types](basic-types/intro.md)
|
||||
- [Numbers](basic-types/numbers.md)
|
||||
- [Char, Bool and Unit](basic-types/char-bool-unit.md)
|
||||
- [Statements and Expressions](basic-types/statements-expressions.md)
|
||||
- [Functions](basic-types/functions.md)
|
||||
- [Ownership and Borrowing](ownership/intro.md)
|
||||
- [Ownership](ownership/ownership.md)
|
||||
- [Reference and Borrowing](ownership/borrowing.md)
|
||||
- [Compound Types](compound-types/intro.md)
|
||||
- [string and slice](compound-types/string-slice.md)
|
||||
- [tuple](compound-types/tuple.md)
|
||||
- [struct](compound-types/struct.md)
|
||||
- [enum](compound-types/enum.md)
|
||||
- [array](compound-types/array.md)
|
||||
- [Flow Control](flow-control.md)
|
||||
- [Pattern Match](pattern-match/intro.md)
|
||||
- [match, if let](pattern-match/match-iflet.md)
|
||||
- [Option destructing](pattern-match/option.md)
|
||||
- [Patterns](pattern-match/patterns.md)
|
||||
- [Method](method.md)
|
||||
- [Generics and Traits](generics-traits/intro.md)
|
||||
- [Generics](generics-traits/generics.md)
|
||||
- [Traits](generics-traits/traits.md)
|
||||
- [Trait Object](generics-traits/trait-object.md)
|
||||
- [Advance Traits](generics-traits/advance-traits.md)
|
||||
- [Collection Types](collections/intro.md)
|
||||
- [Vector](collections/vector.md)
|
||||
- [HashMap](collections/hashmap.md)
|
||||
- [Type Conversion](type-conversion.md)
|
||||
- [Result and panic](result-panic/intro.md)
|
||||
- [panic!](result-panic/panic.md)
|
||||
- [result and ?](result-panic/result.md)
|
||||
- [Crate and module](crate-module/intro.md)
|
||||
- [Crate](crate-module/crate.md)
|
||||
- [Module](crate-module/module.md)
|
||||
- [use and pub](crate-module/use-pub.md)
|
||||
- [Comments and Docs](comments-docs.md)
|
||||
- [Formatted output](formatted-output.md)
|
||||
- [Lifetime](lifetime/intro.md)
|
||||
- [basic](lifetime/basic.md)
|
||||
- [&'static and T: 'static](lifetime/static.md)
|
||||
- [advance](lifetime/advance.md)
|
||||
- [Functional programing](functional-programing/intro.md)
|
||||
- [Closure](functional-programing/cloure.md)
|
||||
- [Iterator](functional-programing/iterator.md)
|
||||
- [newtype and Sized](newtype-sized.md)
|
||||
- [Smart pointers](smart-pointers/intro.md)
|
||||
- [Box](smart-pointers/box.md)
|
||||
- [Deref](smart-pointers/deref.md)
|
||||
- [Drop](smart-pointers/drop.md)
|
||||
- [Rc and Arc](smart-pointers/rc-arc.md)
|
||||
- [Cell and RefCell](smart-pointers/cell-refcell.md)
|
||||
- [Weak and Circle reference](weak.md)
|
||||
- [Self referential](self-referential.md)
|
||||
- [Threads](threads/intro.md)
|
||||
- [Basic using](threads/basic-using.md)
|
||||
- [Message passing](threads/message-passing.md)
|
||||
- [Sync](threads/sync.md)
|
||||
- [Atomic](threads/atomic.md)
|
||||
- [Send and Sync](threads/send-sync.md)
|
||||
- [Global variables](global-variables.md)
|
||||
- [Errors](errors.md)
|
||||
- [unsafe](unsafe.md)
|
||||
- [macro](macro.md)
|
||||
- [Tests](tests/intro.md)
|
||||
- [Write Tests](tests/write-tests.md)
|
||||
- [Benchmark](tests/benchmark.md)
|
||||
- [Unit and Integration](tests/unit-integration.md)
|
||||
- [Assertions](tests/assertions.md)
|
||||
- [Async/Await](async/intro.md)
|
||||
- [async and await!](async/async-await.md)
|
||||
- [Future](async/future.md)
|
||||
- [Pin and Unpin](async/pin-unpin.md)
|
||||
- [Stream](async/stream.md)
|
||||
|
@ -1 +0,0 @@
|
||||
# async and await!
|
@ -1 +0,0 @@
|
||||
# Future
|
@ -1 +0,0 @@
|
||||
# Async/Await
|
@ -1 +0,0 @@
|
||||
# Pin and Unpin
|
@ -1 +0,0 @@
|
||||
# Stream
|
@ -1 +0,0 @@
|
||||
# Char, Bool and Unit
|
@ -1 +0,0 @@
|
||||
# Functions
|
@ -1 +0,0 @@
|
||||
# Basic Types
|
@ -1 +0,0 @@
|
||||
# Numbers
|
@ -1 +0,0 @@
|
||||
# Statements and Expressions
|
@ -1 +0,0 @@
|
||||
# Weak and Circle reference
|
@ -1 +0,0 @@
|
||||
# Circle reference and Self referential
|
@ -1 +0,0 @@
|
||||
# HashMap
|
@ -1 +0,0 @@
|
||||
# Collection Types
|
@ -1 +0,0 @@
|
||||
# Vector
|
@ -1 +0,0 @@
|
||||
# Comments and Docs
|
@ -1 +0,0 @@
|
||||
# array
|
@ -1 +0,0 @@
|
||||
# enum
|
@ -1 +0,0 @@
|
||||
# Compound Types
|
@ -1 +0,0 @@
|
||||
# string and slice
|
@ -1 +0,0 @@
|
||||
# struct
|
@ -1 +0,0 @@
|
||||
# tuple
|
@ -1 +0,0 @@
|
||||
# Crate
|
@ -1 +0,0 @@
|
||||
# Crate
|
@ -1 +0,0 @@
|
||||
# Crate and module
|
@ -1 +0,0 @@
|
||||
# Module
|
@ -1 +0,0 @@
|
||||
# use and pub
|
@ -1 +0,0 @@
|
||||
# Errors
|
@ -1 +0,0 @@
|
||||
# Flow Control
|
@ -1 +0,0 @@
|
||||
# Formatted output
|
@ -1,51 +0,0 @@
|
||||
# Closure
|
||||
|
||||
下面代码是Rust圣经课程中[闭包](http://course.rs/advance/functional-programing/closure.html#结构体中的闭包)章节的课内练习题答案:
|
||||
|
||||
```rust
|
||||
struct Cacher<T,E>
|
||||
where
|
||||
T: Fn(E) -> E,
|
||||
E: Copy
|
||||
{
|
||||
query: T,
|
||||
value: Option<E>,
|
||||
}
|
||||
|
||||
impl<T,E> Cacher<T,E>
|
||||
where
|
||||
T: Fn(E) -> E,
|
||||
E: Copy
|
||||
{
|
||||
fn new(query: T) -> Cacher<T,E> {
|
||||
Cacher {
|
||||
query,
|
||||
value: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn value(&mut self, arg: E) -> E {
|
||||
match self.value {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
let v = (self.query)(arg);
|
||||
self.value = Some(v);
|
||||
v
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn main() {
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn call_with_different_values() {
|
||||
let mut c = Cacher::new(|a| a);
|
||||
|
||||
let v1 = c.value(1);
|
||||
let v2 = c.value(2);
|
||||
|
||||
assert_eq!(v2, 1);
|
||||
}
|
||||
```
|
@ -1 +0,0 @@
|
||||
# Functional programing
|
@ -1 +0,0 @@
|
||||
# Iterator
|
@ -1,51 +0,0 @@
|
||||
# Closure
|
||||
|
||||
下面代码是Rust圣经课程中[闭包](http://course.rs/advance/functional-programing/closure.html#结构体中的闭包)章节的课内练习题答案:
|
||||
|
||||
```rust
|
||||
struct Cacher<T,E>
|
||||
where
|
||||
T: Fn(E) -> E,
|
||||
E: Copy
|
||||
{
|
||||
query: T,
|
||||
value: Option<E>,
|
||||
}
|
||||
|
||||
impl<T,E> Cacher<T,E>
|
||||
where
|
||||
T: Fn(E) -> E,
|
||||
E: Copy
|
||||
{
|
||||
fn new(query: T) -> Cacher<T,E> {
|
||||
Cacher {
|
||||
query,
|
||||
value: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn value(&mut self, arg: E) -> E {
|
||||
match self.value {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
let v = (self.query)(arg);
|
||||
self.value = Some(v);
|
||||
v
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn main() {
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn call_with_different_values() {
|
||||
let mut c = Cacher::new(|a| a);
|
||||
|
||||
let v1 = c.value(1);
|
||||
let v2 = c.value(2);
|
||||
|
||||
assert_eq!(v2, 1);
|
||||
}
|
||||
```
|
@ -1 +0,0 @@
|
||||
# Functional Programming
|
@ -1 +0,0 @@
|
||||
# Advance Traits
|
@ -1 +0,0 @@
|
||||
# Generics
|
@ -1 +0,0 @@
|
||||
# Generics and Traits
|
@ -1 +0,0 @@
|
||||
# Trait Object
|
@ -1 +0,0 @@
|
||||
# Traits
|
@ -1 +0,0 @@
|
||||
# Global variables
|
@ -1 +0,0 @@
|
||||
# advance
|
@ -1,26 +0,0 @@
|
||||
## 生命周期消除
|
||||
|
||||
```rust
|
||||
fn print(s: &str); // elided
|
||||
fn print<'a>(s: &'a str); // expanded
|
||||
|
||||
fn debug(lvl: usize, s: &str); // elided
|
||||
fn debug<'a>(lvl: usize, s: &'a str); // expanded
|
||||
|
||||
fn substr(s: &str, until: usize) -> &str; // elided
|
||||
fn substr<'a>(s: &'a str, until: usize) -> &'a str; // expanded
|
||||
|
||||
fn get_str() -> &str; // ILLEGAL
|
||||
|
||||
fn frob(s: &str, t: &str) -> &str; // ILLEGAL
|
||||
|
||||
fn get_mut(&mut self) -> &mut T; // elided
|
||||
fn get_mut<'a>(&'a mut self) -> &'a mut T; // expanded
|
||||
|
||||
fn args<T: ToCStr>(&mut self, args: &[T]) -> &mut Command // elided
|
||||
fn args<'a, 'b, T: ToCStr>(&'a mut self, args: &'b [T]) -> &'a mut Command // expanded
|
||||
|
||||
fn new(buf: &mut [u8]) -> BufWriter; // elided
|
||||
fn new(buf: &mut [u8]) -> BufWriter<'_>; // elided (with `rust_2018_idioms`)
|
||||
fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a> // expanded
|
||||
```
|
@ -1 +0,0 @@
|
||||
# Lifetime
|
@ -1,49 +0,0 @@
|
||||
# &'static and T: 'static
|
||||
|
||||
```rust,editable
|
||||
use std::fmt::Display;
|
||||
|
||||
fn main() {
|
||||
let mut string = "First".to_owned();
|
||||
|
||||
string.push_str(string.to_uppercase().as_str());
|
||||
print_a(&string);
|
||||
print_b(&string);
|
||||
print_c(&string); // Compilation error
|
||||
print_d(&string); // Compilation error
|
||||
print_e(&string);
|
||||
print_f(&string);
|
||||
print_g(&string); // Compilation error
|
||||
}
|
||||
|
||||
fn print_a<T: Display + 'static>(t: &T) {
|
||||
println!("{}", t);
|
||||
}
|
||||
|
||||
fn print_b<T>(t: &T)
|
||||
where
|
||||
T: Display + 'static,
|
||||
{
|
||||
println!("{}", t);
|
||||
}
|
||||
|
||||
fn print_c(t: &'static dyn Display) {
|
||||
println!("{}", t)
|
||||
}
|
||||
|
||||
fn print_d(t: &'static impl Display) {
|
||||
println!("{}", t)
|
||||
}
|
||||
|
||||
fn print_e(t: &(dyn Display + 'static)) {
|
||||
println!("{}", t)
|
||||
}
|
||||
|
||||
fn print_f(t: &(impl Display + 'static)) {
|
||||
println!("{}", t)
|
||||
}
|
||||
|
||||
fn print_g(t: &'static String) {
|
||||
println!("{}", t);
|
||||
}
|
||||
```
|
@ -1 +0,0 @@
|
||||
# macro
|
@ -1 +0,0 @@
|
||||
# Method
|
@ -1 +0,0 @@
|
||||
# newtype and Sized
|
@ -1 +0,0 @@
|
||||
# Reference and Borrowing
|
@ -1 +0,0 @@
|
||||
# Ownership and Borrowing
|
@ -1 +0,0 @@
|
||||
# Ownership
|
@ -1 +0,0 @@
|
||||
# Pattern Match
|
@ -1 +0,0 @@
|
||||
# match, if let
|
@ -1 +0,0 @@
|
||||
# Option destructing
|
@ -1 +0,0 @@
|
||||
# Patterns
|
@ -1 +0,0 @@
|
||||
# panic!
|
@ -1 +0,0 @@
|
||||
# Result and panic
|
@ -1 +0,0 @@
|
||||
# panic!
|
@ -1 +0,0 @@
|
||||
# result and ?
|
@ -1 +0,0 @@
|
||||
# Self referential
|
@ -1 +0,0 @@
|
||||
# Box
|
@ -1 +0,0 @@
|
||||
# Cell and RefCell
|
@ -1 +0,0 @@
|
||||
# Deref
|
@ -1 +0,0 @@
|
||||
# Drop
|
@ -1 +0,0 @@
|
||||
# Smart pointers
|
@ -1 +0,0 @@
|
||||
# Rc and Arc
|
@ -1 +0,0 @@
|
||||
# Assertions
|
@ -1,3 +0,0 @@
|
||||
# Benchmark
|
||||
|
||||
https://doc.rust-lang.org/unstable-book/library-features/test.html
|
@ -1 +0,0 @@
|
||||
# Tests
|
@ -1 +0,0 @@
|
||||
# Unit and Integration
|
@ -1 +0,0 @@
|
||||
# Write Tests
|
@ -1 +0,0 @@
|
||||
# Atomic
|
@ -1 +0,0 @@
|
||||
# Basic using
|
@ -1 +0,0 @@
|
||||
# Threads
|
@ -1 +0,0 @@
|
||||
# Message passing
|
@ -1 +0,0 @@
|
||||
# Send and Sync
|
@ -1 +0,0 @@
|
||||
# Sync
|
@ -1 +0,0 @@
|
||||
# Type Conversion
|
@ -1 +0,0 @@
|
||||
# unsafe
|
@ -1 +0,0 @@
|
||||
# Variables
|
@ -1 +0,0 @@
|
||||
# Weak and Circle reference
|
@ -1 +0,0 @@
|
||||
# Why Exercise.rs
|
Reference in New Issue
Block a user