Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[READY] Update example 2_4 and 3_1 #53

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions 2_idioms/2_4_generic_in_type_out/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ The common and obvious rules in [Rust] when choosing type for an input parameter

Let's illustrate it with the following trivial examples:
```rust
use derive_more::{AsRef, AsMut};

// Read-only access is enough here.
pub fn just_print_stringy(v: &str) {
println!("{}", v)
Expand All @@ -25,6 +27,8 @@ pub fn add_hi(v: &mut String) {

#[derive(AsMut, AsRef)]
pub struct Nickname(String);


impl Nickname {
// We want to own `nickname` inside `Nickname` value.
pub fn new(nickname: String) -> Self {
Expand All @@ -41,19 +45,25 @@ just_print_stringy(nickname.as_ref());

The most standard way to improve ergonomics here is to __hide type conversions under-the-hood by abstracting over input types__ in our APIs:
```rust
pub fn just_print_stringy<S: AsRef<str>>(v: S) {
use derive_more::{AsRef, AsMut};

pub fn just_print_stringy<S: AsRef<String>>(v: S) {
println!("{}", v.as_ref())
}

pub fn add_hi<S: AsMut<String>>(v: S) {
pub fn add_hi<S: AsMut<String>>(mut v: S) {
v.as_mut().push_str(" Hi")
}

impl Nickname {
#[derive(AsMut, AsRef)]
pub struct Nickname(String);

impl Nickname {
pub fn new<S: Into<String>>(nickname: S) -> Self {
Self(nickname.into())
}
}

```
And now our API is pleasant to use:
```rust
Expand Down
10 changes: 10 additions & 0 deletions 3_ecosystem/3_1_testing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ For better understanding and familiarity with [fuzzing][31] in [Rust], read thro
- [Integrated tests are scam (J.B. Rainsberg)][61]
- [nextest][62]

## CLI Testing
- [assert_cmd](https://crates.io/crates/assert_cmd) - Easy command initialization and assertions.
- [assert_fs](https://crates.io/crates/assert_fs) - Filesystem fixtures and assertions for testing.
- [predicates](https://crates.io/crates/predicates) - Composable first-order predicate functions.
- [rexpect](https://crates.io/crates/rexpect) - For interactive CLI testing.
- [CLI testing](https://rust-cli.github.io/book/tutorial/testing.html)
- [example with rexpect](https://www.rustadventure.dev/building-a-digital-garden-cli/clap-v4/testing-interactive-clis-with-rexpect)
- [one more example](https://out-of-cheese-error.netlify.app/the-way)



## Task

Expand Down
Loading