Skip to content
Example Rust CLI program that calculates the next prime after a given number
Rust
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
Cargo.lock
Cargo.toml
README.md

README.md

Next Prime CLI

Usage

Build the program and run it like a normal CLI:

cargo build
./target/debug/next-prime-rs -h
./target/debug/next-prime-rs -V
./target/debug/next-prime-rs 1

Crates You Should Know

  • Clap - Command Line Argument Parser for Rust

  • Primes - A prime generator for Rust.

Learnings

Return Values

  • Forgot the return type (-> u64)

  • I was trying to return using a statement: n; vs n

Statements are instructions that perform some action and do not return a value. Expressions evaluate to a resulting value.

error[E0308]: mismatched types
 --> src/main.rs:4:26
  |
4 | fn next_prime(x: u64) -> u64 {
  |    ----------            ^^^ expected u64, found ()
  |    |
  |    implicitly returns `()` as its body has no tail or `return` expression
...
7 |     n;
  |      - help: consider removing this semicolon
  |
  = note: expected type `u64`
             found type `()`
You can’t perform that action at this time.