parent
880d779e6c
commit
c52caca844
@ -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"
|
||||
|
||||
@ -1 +1,2 @@
|
||||
pub mod errors;
|
||||
pub mod vec;
|
||||
|
||||
1
subject_source/src/vec.rs
Normal file
1
subject_source/src/vec.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod access;
|
||||
10
subject_source/src/vec/access.rs
Normal file
10
subject_source/src/vec/access.rs
Normal file
@ -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<f32> {
|
||||
match v.last_chunk() {
|
||||
Some([a, b]) => Some(a + b),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
11
subject_source/tests/vec_access.rs
Normal file
11
subject_source/tests/vec_access.rs
Normal file
@ -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));
|
||||
}
|
||||
@ -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.
|
||||
|
||||
@ -5,4 +5,7 @@
|
||||
│ ├── option.rs
|
||||
│ └── result.rs
|
||||
├── errors.rs
|
||||
├── vec
|
||||
│ └── access.rs
|
||||
├── vec.rs
|
||||
└── lib.rs
|
||||
|
||||
34
subject_text/vec/index.md
Normal file
34
subject_text/vec/index.md
Normal file
@ -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<T>` 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<T>`.
|
||||
```
|
||||
|
||||
```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<f32> {
|
||||
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));
|
||||
}
|
||||
```
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user