Program that adds one to a number

Hi :wave:,

This is my first Rust program, I would love some feedback :grinning:

https://github.com/02sh/add-one

Thanks!

You didn't handle the case where no arguments are provided. Also, you don't appear to be using rustfmt or clippy. Also also, you don't appear to be using continuous integration to ensure your code continues to compile with supported versions of the language (which you also haven't specified). Not to mention the extremely thin documentation.

4.75/10, would not recommend for enterprise use. Goes well with a nice, sharp Merlot and maybe some Camembert.

2 Likes

Thanks for the feedback, I've just added the check to exit when no arguments. I do use clappy (cargo +nightly clippy) and everything seems OK. The CI will come soon, and I specified the version of Rust required.

Concerning the documentation. I don't see what more I can say...:thinking:

Thanks again :clap:

You can rewrite your add_one function as

fn add_one(number_str: &str) -> Result<BigInt, ParseBigIntError> {
    number_str.parse::<BigInt>().map(|n| n + 1)
}

Also I must say naming variables like o or i is not the best practice.

Thanks, this is better :clap:
I've changed the variable names to something more explicit.

I added the repository to travis for continuous integration.
I also published the project to crates

Capture

It is possible (since rustc 1.26) to indicate, whether the program was successful or not:

fn main() -> Result<(),()> {
    return Ok(());
    // return Err(());
}

I am sorry, but I really don't see how I could use this for the main function :sweat:

In traditional shell programming, add-one could be a component of a larger system. If the input was erroneous, you might want to tell this to the caller in order that it can throw an exception.

Restricting your program to human readable output, there are other interesting ways to achieve this. For example, "add-one 1" could print an S-expression to stdout:

    (ok 2)

or a JSON representation of it:

    ["ok",2]

I understand, but the program is intended to be used both as a binary and a library, I don't think this would be relevant for the latter, I may be wrong though.:disappointed_relieved:

update: the add_one function has been improved, no more BigInt. The input is considered as a list of u8 and only the last digit(s) will have to be incremented/decremented and anything before is left unchanged. This should make for better performance, thanks to github.com/m-ou-se for the PR :hugs:

Let's take you to the next step of learning:

How about writing some documentation above the function (in docblocks) and generating neat documentation using cargo doc ?

Currently your documentation is empty

Be careful with std::process::exit, it prevents the program from running destructors (drop), thus valgrind will start to grumble about memory leak. Destructors are only called while returning from a function, not when diving into exit. That said, I would recommend the following safer solution:

use std::process::exit;

fn fmain() -> Result<(),()> {
    return Ok(());
    // return Err(());
}

fn main() {
    if fmain().is_err() {exit(1);}
}

Cargo doc is a great tool, thanks for telling me about it :ok_hand:

doc

here is the documentation

1 Like

Reminder that you can just have main return a Result these days: Announcing Rust 1.26 | Rust Blog

1 Like