Rust `Fn` traits and composition – Episode 70 of Unhindered by Coding
Channel: Unhindered by Coding!
Date: 2023-02-22

We spent the first half hour or so of today's session going over what some notes and diagrams I'd made, where I was trying to make sure I understood the basic ideas in esitsu@'sTwitch's proposed design for our recombination tools. I don't know if anyone else found that useful, but it certainly helped clarify things for _me_.

In particular, I hadn't really appreciated that esitsu's `.then()` method was essentially the same as `.compose()`, and that their `Operator` type was _very_ similar to the existing `Fn` type. Going through all this helped me understand those relationship, and ask a bunch of new questions.

Then we hit the Big Problem where code that we thought should be fine wouldn't compile. The code in the stream is reasonably complex, but the problem is illustrated by this simplified version:

```rust
fn compose(A, B, C, F, G)(f: F, g: G) -} impl Fn(A) -} C
where
 F: Fn(A) -} B,
 G: Fn(B) -} C,
{
 move |x| g(f(x))
}

fn main() {
 let increment = |x: i32| x + 1;
 let first = |vals: &[i32]| vals[0];

 let first_plus_one = compose(first, increment);

 let vals = vec![5, 8, 9];

 let result = first_plus_one(&vals);
 assert_eq!(6, result);
}
```

We collectively flailed on this for quite a while, and the stream ended with a decision that I would post this question on the Rust users group and see if someone there could help us. (Spoiler: They did and we were able to move forward in the next EC session, episode 73.)

================

In the first few months of 2023 (at least to mid-March) I'm streaming at https://twitch.tv/NicMcPhee four times a week:

* Tuesday: 10am-noon CST
* Wednesday: 7-9pm CST
* Saturday: 2-4pm CST
* Sunday: 10am-noon CST

The Tuesday and Sunday slots will be focusing on building the EC system in Rust and on the ice-repos webapp. The Wednesday and Saturday slots will be going through the Advent of Code 2022 puzzles/exercises.

Thanks for watching!