mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-01-18 20:39:35 +02:00
Replace binary_search
example with try_into
(#1213)
I don't think `binary_search` is the best example of using `Result` — it should actually use an `Either` type instead.
This commit is contained in:
parent
38557b142a
commit
ddc9ea1fa6
@ -8,8 +8,8 @@ fn main() {
|
||||
let first: Option<&i8> = numbers.first();
|
||||
println!("first: {first:?}");
|
||||
|
||||
let idx: Result<usize, usize> = numbers.binary_search(&10);
|
||||
println!("idx: {idx:?}");
|
||||
let arr: Result<[i8; 3], Vec<i8>> = numbers.try_into();
|
||||
println!("arr: {arr:?}");
|
||||
}
|
||||
```
|
||||
|
||||
@ -18,8 +18,8 @@ fn main() {
|
||||
* `Option` and `Result` are widely used not just in the standard library.
|
||||
* `Option<&T>` has zero space overhead compared to `&T`.
|
||||
* `Result` is the standard type to implement error handling as we will see on Day 3.
|
||||
* `binary_search` returns `Result<usize, usize>`.
|
||||
* If found, `Result::Ok` holds the index where the element is found.
|
||||
* Otherwise, `Result::Err` contains the index where such an element should be inserted.
|
||||
* `try_into` attempts to convert the vector into a fixed-sized array. This can fail:
|
||||
* If the vector has the right size, `Result::Ok` is returned with the array.
|
||||
* Otherwise, `Result::Err` is returned with the original vector.
|
||||
|
||||
</details>
|
||||
|
Loading…
Reference in New Issue
Block a user