From 664ab08a3190b28f3e632b460c4530c7bc5307b6 Mon Sep 17 00:00:00 2001 From: lilymonade Date: Tue, 11 Mar 2025 17:45:10 +0000 Subject: [PATCH 1/4] yay fixed (#2) Reviewed-on: https://gitea.lilymonade.fr/lilymonade/exowos/pulls/2 --- subject_text/tree | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/subject_text/tree b/subject_text/tree index af5deae..9681cf5 100644 --- a/subject_text/tree +++ b/subject_text/tree @@ -2,6 +2,7 @@ ├── Cargo.toml └── src    ├── errors -    │   └── option.rs +    │   ├── option.rs +    │   └── result.rs    ├── errors.rs    └── lib.rs From cb59d215cb6b922a2d91f046787d9ac1b5284338 Mon Sep 17 00:00:00 2001 From: lilymonade Date: Tue, 11 Mar 2025 18:49:18 +0100 Subject: [PATCH 2/4] up revision --- subject_text/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subject_text/index.md b/subject_text/index.md index 51b8925..4e7e358 100644 --- a/subject_text/index.md +++ b/subject_text/index.md @@ -1,5 +1,5 @@ --- -revision = "0.1.0" +revision = "0.2.0" parts = ["errors"] --- From 880d779e6c98bc8962be69896d31f3a48be5c7f3 Mon Sep 17 00:00:00 2001 From: lilymonade Date: Tue, 11 Mar 2025 18:57:14 +0100 Subject: [PATCH 3/4] big flemme faire une pr pour ca --- subject_text/errors/result.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subject_text/errors/result.md b/subject_text/errors/result.md index f1769d4..8d0e38f 100644 --- a/subject_text/errors/result.md +++ b/subject_text/errors/result.md @@ -6,7 +6,7 @@ file = "src/errors/result.rs" We saw earlier the `Option` type, which represents a possibly absent value (sometimes called "nullable" in other languages). Now it would be useful to carry **the reason** of this absence of value. Let's say you are parsing an integer from a `String`, there are multiple ways it can fail. - The string contains invalid characters (non-digits). -- The value we want to parse is too large (for example, trying to parse `"999999999999"` as an i32). +- The value we want to parse is too large (for example, trying to parse `"999999999999"` as an `i32`). If we want to react properly, we need to know why it failed. So instead of having only `None` as output, we would like a variant like `SomeError(v)`. In Rust, this type with `Some` and `SomeError` variants is called [`Result`](https://doc.rust-lang.org/std/result/). From a00350bc0e82cb5789c3d786b0eb9999e4ba2d70 Mon Sep 17 00:00:00 2001 From: lilymonade Date: Tue, 11 Mar 2025 22:32:21 +0100 Subject: [PATCH 4/4] first draft --- practical.yml | 7 ++++++ subject_source/src/lib.rs | 1 + subject_source/src/vec.rs | 1 + subject_source/src/vec/access.rs | 10 +++++++++ subject_source/tests/vec_access.rs | 11 ++++++++++ subject_text/index.md | 4 ++-- subject_text/tree | 3 +++ subject_text/vec/index.md | 34 ++++++++++++++++++++++++++++++ 8 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 subject_source/src/vec.rs create mode 100644 subject_source/src/vec/access.rs create mode 100644 subject_source/tests/vec_access.rs create mode 100644 subject_text/vec/index.md diff --git a/practical.yml b/practical.yml index c74b803..2988374 100644 --- a/practical.yml +++ b/practical.yml @@ -37,3 +37,10 @@ exercises: - "get_val_or_panic_ok" - "discard_err_ok" - "discard_err_err" + access: + required_files: + - "src/vec.rs" + - "src/vec/access.rs" + tests: + - "add_last_two_not_enough" + - "add_last_two_enough" diff --git a/subject_source/src/lib.rs b/subject_source/src/lib.rs index 629e98f..9eb255a 100644 --- a/subject_source/src/lib.rs +++ b/subject_source/src/lib.rs @@ -1 +1,2 @@ pub mod errors; +pub mod vec; diff --git a/subject_source/src/vec.rs b/subject_source/src/vec.rs new file mode 100644 index 0000000..ce652d9 --- /dev/null +++ b/subject_source/src/vec.rs @@ -0,0 +1 @@ +pub mod access; diff --git a/subject_source/src/vec/access.rs b/subject_source/src/vec/access.rs new file mode 100644 index 0000000..16ab63a --- /dev/null +++ b/subject_source/src/vec/access.rs @@ -0,0 +1,10 @@ +/// Add the last two numbers of the input slice. +/// +/// If the slice is not large enough, return `None` +/// If it is, return the computed value in a `Some` +pub fn add_last_two(v: &[f32]) -> Option { + match v.last_chunk() { + Some([a, b]) => Some(a + b), + None => None, + } +} diff --git a/subject_source/tests/vec_access.rs b/subject_source/tests/vec_access.rs new file mode 100644 index 0000000..376a2ee --- /dev/null +++ b/subject_source/tests/vec_access.rs @@ -0,0 +1,11 @@ +use subject_source::vec::access; + +#[test] +pub fn add_last_two_not_enough() { + assert_eq!(access::add_last_two(&[1.0]), None); +} + +#[test] +pub fn add_last_two_enough() { + assert_eq!(access::add_last_two(&[1.0, 2.0, 3.0]), Some(5.0)); +} diff --git a/subject_text/index.md b/subject_text/index.md index 4e7e358..0fe0647 100644 --- a/subject_text/index.md +++ b/subject_text/index.md @@ -1,6 +1,6 @@ --- -revision = "0.2.0" -parts = ["errors"] +revision = "0.3.0" +parts = ["errors", "vec"] --- When it comes to programming, it's all fun and games until the real world comes in and sends weird unexpected inputs to your little protege. So you better handle those cases as best as you can. There are 3 main ways of handling errors. diff --git a/subject_text/tree b/subject_text/tree index 9681cf5..3f6193c 100644 --- a/subject_text/tree +++ b/subject_text/tree @@ -5,4 +5,7 @@    │   ├── option.rs    │   └── result.rs    ├── errors.rs +    ├── vec +    │   └── access.rs +    ├── vec.rs    └── lib.rs diff --git a/subject_text/vec/index.md b/subject_text/vec/index.md new file mode 100644 index 0000000..af20326 --- /dev/null +++ b/subject_text/vec/index.md @@ -0,0 +1,34 @@ +--- +name = "Vecs and slices" +difficulty = 2 +exercises = ["access.md"] +--- + +Let's now look at some functions on [`slice`](https://doc.rust-lang.org/std/primitive.slice.html)s and [`Vec`](https://doc.rust-lang.org/std/vec/struct.Vec.html)s. Instead of manualy checking things we will follow the type system using `Option`s and `Result`s we saw earlier. + +```note +Slices (`[T]`) represent some memory space containing an arbitrary number of elements of type `T`. Since they don't have a size known at compilation time, we can only access them through pointers, commonly `&[T]` (references to slices). +``` + +```deepening +`Vec` can be seen as [owned](https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html) `[T]`, it means that every function working on a `&[T]` can work on a `&Vec`. +``` + +```prototype +/// Add the last two numbers of the input slice. +/// +/// If the slice is not large enough, return `None` +/// If it is, return the computed value in a `Some` +pub fn add_last_two(v: &[f32]) -> Option { + unimplemented!() +} +``` + +```example +fn main() { + assert_eq!(add_last_two(&[]), None); + assert_eq!(add_last_two(&[10.0]), None); + assert_eq!(add_last_two(&[1.0, 2.0, 3.0]), Some(5.0)); +} +``` +