Skip to content

Commit

Permalink
autofmt doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehforsch committed Feb 25, 2024
1 parent c6dcc25 commit f6a099e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,18 @@ assert_eq!(length, 5.0 * kilometers); // This only holds in SI units!
```
The combination of `value_unchecked` and `new_unchecked` comes in handy when using third party libraries that only takes the raw storage type as argument. As an example, suppose we have a function `foo` that takes a `Vec<f64>` and returns a `Vec<f64>`, and suppose it sorts the numbers or does some other unit safe operation. Then we could reasonably write:
```rust
let lengths: Vec<Length<f64>> = vec![1.0 * meters, 2.0 * kilometers, 3.0 * meters, 4.0 * kilometers];
let unchecked = lengths
.into_iter()
.map(|x| x.value_unchecked())
.collect();
let fooed = foo(unchecked);
let result: Vec<_> = fooed.into_iter().map(|x| Length::new_unchecked(x)).collect();
let lengths: Vec<Length<f64>> = vec![
1.0 * meters,
2.0 * kilometers,
3.0 * meters,
4.0 * kilometers,
];
let unchecked = lengths.into_iter().map(|x| x.value_unchecked()).collect();
let fooed = foo(unchecked);
let result: Vec<_> = fooed
.into_iter()
.map(|x| Length::new_unchecked(x))
.collect();
```
## Debug
`Debug` is implemented and will print the quantity in its base representation.
Expand Down
19 changes: 12 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,18 @@
//! # fn foo(x: Vec<f64>) -> Vec<f64> {
//! # x
//! # }
//! let lengths: Vec<Length<f64>> = vec![1.0 * meters, 2.0 * kilometers, 3.0 * meters, 4.0 * kilometers];
//! let unchecked = lengths
//! .into_iter()
//! .map(|x| x.value_unchecked())
//! .collect();
//! let fooed = foo(unchecked);
//! let result: Vec<_> = fooed.into_iter().map(|x| Length::new_unchecked(x)).collect();
//! let lengths: Vec<Length<f64>> = vec![
//! 1.0 * meters,
//! 2.0 * kilometers,
//! 3.0 * meters,
//! 4.0 * kilometers,
//! ];
//! let unchecked = lengths.into_iter().map(|x| x.value_unchecked()).collect();
//! let fooed = foo(unchecked);
//! let result: Vec<_> = fooed
//! .into_iter()
//! .map(|x| Length::new_unchecked(x))
//! .collect();
//! ```
//! ## Debug
//! `Debug` is implemented and will print the quantity in its base representation.
Expand Down

0 comments on commit f6a099e

Please sign in to comment.