linked
All checks were successful
buildbot/mdbook test Build done.

This commit is contained in:
AF 2024-02-14 19:28:00 +00:00
parent 850283de10
commit cbcee48b40
2 changed files with 69 additions and 0 deletions

View File

@ -12,6 +12,7 @@
- [RcChars](./exercises/rcchars.md) - [RcChars](./exercises/rcchars.md)
- [Async Fn](./exercises/async_fn.md) - [Async Fn](./exercises/async_fn.md)
- [Get Functions](./exercises/get_functions.md) - [Get Functions](./exercises/get_functions.md)
- [Linked](./exercises/linked.md)
- [Chapter 2](./chapter_2.md) - [Chapter 2](./chapter_2.md)
- [AnyStr](./exercises/anystr.md) - [AnyStr](./exercises/anystr.md)
- [Mode](./exercises/mode.md) - [Mode](./exercises/mode.md)

68
src/exercises/linked.md Normal file
View File

@ -0,0 +1,68 @@
# `Linked`
Fix (many) errors to make this compile and pass tests:
```rust
# #[derive(Clone, Copy)]
struct Node<'a> {
next: Option<&'a Self>,
value: &'a str,
}
struct Linked<'a, F> {
node: Node<'a>,
callback: &'a mut F,
}
impl<'a, F: FnMut(String)> Linked<'a, F> {
# /*
fn with(&mut self, value: &str) -> Linked<'_, F> {
Self {
node: Node {
next: Some(self.node),
# */
# fn with<'b>(&'b mut self, value: &'b str) -> Linked<'b, F> {
# Linked {
# node: Node {
# next: Some(&self.node),
value,
},
callback: self.callback,
}
}
fn call(&mut self) {
let mut node = self.node;
let mut value = node.value.to_string();
while let Some(next) = node.next {
value += next.value;
# /*
node = next;
}
self.callback(value);
# */
# node = *next; }
# (self.callback)(value);
}
}
let mut vec = vec![];
let mut linked = Linked {
node: Node { next: None, value: "0" },
callback: &mut |value| vec.push(value),
};
// why does removing code below cause the instantiation to fail?
linked.call();
{
let mut linked = linked.with("1");
linked.with("2").call();
linked.with("3").call();
}
linked.with("4").call();
assert_eq!(vec, ["0", "210", "310", "40"]);
```