MCPcopy Create free account
hub / github.com/Apress/beginning-rust-2e / try_from

Method try_from

Source Code/20-16.rs:12–29  ·  view source on GitHub ↗
(source: LargeNumber)

Source from the content-addressed store, hash-verified

10 impl TryFrom<LargeNumber> for SmallNumber {
11 type Error = String;
12 fn try_from(source: LargeNumber) -> Result<Self, Self::Error> {
13 if source.0.abs() > f32::MAX as f64 {
14 Err("too large number".to_string())
15 }
16 else if source.0 != 0.
17 && source.0.abs() < f32::MIN_POSITIVE as f64 {
18 Err("too small number".to_string())
19 }
20 else {
21 let result = source.0 as f32;
22 if result as f64 == source.0 {
23 Ok(Self (result))
24 }
25 else {
26 Err("precision loss".to_string())
27 }
28 }
29 }
30 }
31 fn show_result(n: Result<SmallNumber, String>) {
32 match n {

Callers

nothing calls this directly

Calls 1

absMethod · 0.45

Tested by

no test coverage detected