Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor of parsing arguments into correct types #4

Closed
leggettc18 opened this issue Jun 7, 2019 · 7 comments
Closed

Refactor of parsing arguments into correct types #4

leggettc18 opened this issue Jun 7, 2019 · 7 comments
Assignees
Milestone

Comments

@leggettc18
Copy link
Owner

In GitLab by @leggettc18 on Sep 27, 2018, 18:14

/u/usernamedottxt on Reddit said

This:

let goal = if goal.is_some() {
        Some(goal.unwrap().parse::<f64>()?)
    } else {
        None
    };

Can be written as:

let goal = goal.map_or(None, |x| x.parse::<f64>())?;

https://www.reddit.com/r/rust/comments/9jdjpc/my_new_project_in_rust_open_to_critiques_and/e6qno3f

@leggettc18 leggettc18 added this to the 1.0.0 milestone Jun 7, 2019
@leggettc18 leggettc18 self-assigned this Jun 7, 2019
@leggettc18
Copy link
Owner Author

In GitLab by @leggettc18 on Sep 27, 2018, 18:23

changed the description

@leggettc18
Copy link
Owner Author

In GitLab by @leggettc18 on Sep 27, 2018, 22:54

changed the description

@leggettc18
Copy link
Owner Author

In GitLab by @leggettc18 on Sep 28, 2018, 05:50

Upon further investigation, I am not sure this is exactly correct. For a few reasons

  1. The map_or function returns a value of the type of the default value provided, and the mapping function
    should also return a value of that type. However, in the example above, it returns either a None or a
    Result<f64, std::num::ParseFloatError>. The ? then unwraps the Result into just f64 (or returns any parsing error to the calling function). The variable I am assigning the value to is intended to be of type Some(f64)
  2. With the example exactly as it was, I got the error the trait bound std::option::NoneError: std::error::Error is not satisfied. However, it appears that NoneError is an experimental nightly-only API.

At the moment I do not know how to solve these errors, so I have left the functions as they are. I did change them so that they remove the decimal points and save integers instead of floats in !2. I will leave the issue open for now.

@leggettc18
Copy link
Owner Author

In GitLab by @leggettc18 on Oct 2, 2018, 13:14

mentioned in commit 38da5f2

@leggettc18
Copy link
Owner Author

In GitLab by @leggettc18 on Oct 2, 2018, 13:16

mentioned in merge request !6

@leggettc18
Copy link
Owner Author

In GitLab by @leggettc18 on Oct 2, 2018, 13:17

mentioned in commit 132a59d

@leggettc18
Copy link
Owner Author

In GitLab by @leggettc18 on Oct 2, 2018, 13:17

closed via merge request !6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant