Giter VIP home page Giter VIP logo

scanrs's Introduction

scanrs

A dead-simple Rust Library to easily process std input. No dependencies, no strange functionalities, not even useful enough. You can always copy-paste the code inside src/lib.rs.

What is it?

This crate was a little inspired by this, so it won't have any dependencies besides the standard library, anything extra should be done by hand or cherrypicked to avoid bloating something this simple.

https://www.reddit.com/r/rust/comments/dy365h/only_one_wish_for_rust_2020/

2023 update: I've realized the terrible mistake I've made by publishing this. Adding up to the dependency-hell problem because devs are independent-no-more. I'll keep maintaining this "feature complete" crate. However, I plead to you, please, don't be an npm user and get a library for dead-simple things :) checkout rust easy snippets for this

scanrs is a small working rust crate that makes handling user input easier.

Rust has a rather "weird" verbose way to read std input, even though asking for user input inside terminal applications is a little outdated. If you cannot rely on the command-line arguments for reliable input, this crate can prove itself useful.

The other case-scenario in my head is to use this to aid new rustaceans to ask for a proper input, sadly doing this in rust results in long lines of text and the end program turns very verbose (not really).

This library just attempts to make reading standard primitive types (and two more cases) easier which makes it faster, simple to understand and lightweight.

How to use it?

This crate doesn't have much science behind it, to it's fairly simple to use, you just assign a variable and set it's value to the result of the function you wish.

One way of reading user input in rust:

One simple way to read user input in rust is by playing a little with error handling (or using a straight unwrap instead).

let mut number = String::new();

io::stdin().read_line(&mut number).expect("IO Failure");

let number: f64 = match number.trim().parse() {
    Ok(num) => num,
    Err(_) => panic!("Not a number!"),
};

or

let mut number = String::new();
io::stdin().read_line(&mut number).expect("IO Failure");

match input.trim().parse() {
    Ok(number) => number,
    Err(_) => panic!("Invalid input. Please enter a valid number."),
}

However this turns a little verbose when you need to read multiple variables, so you can use this crate instead.

use scanrs::scann;

fn main() {
    println!("Please input a number");
    let num = scann();
    println!("You entered {}", num);
}

What kind of primitive types can it handle?

Current functions available:

  • scann: Reads a number from stdin.
  • scanvec: Scans a whitespace separated list of elements
  • scanln: Scans a String

TODO:

  • Allow users to customize input message. - Dimmed as bloatware
  • Allow users to customize the function error message without introducing a breaking change.
  • Make this crate colored to send fancy errors. - Dimmed as bloatware

scanrs's People

Contributors

dependabot-preview[bot] avatar nik0rasu avatar ventgrey avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

nik0rasu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.