| Crates.io | from-user |
| lib.rs | from-user |
| version | 0.2.3 |
| created_at | 2025-09-14 10:46:28.168599+00 |
| updated_at | 2025-09-15 15:18:22.870236+00 |
| description | A small Rust crate for taking input. Highly Simplified |
| homepage | |
| repository | https://github.com/SharmaDevanshu089/input |
| max_upload_size | |
| id | 1838586 |
| size | 7,416 |
A tiny, highly simplified Rust input helper
Version: 0.2.3
A minimal library that provides two helpers for reading user input from stdin:
single_line() — returns the raw line as a String.number() — tries to read a numeric line and return it as i32 (retries on invalid input).This crate is intentionally small and easy to read — good for learning or small CLI experiments.
Add the crate to your Cargo.toml (when published on crates.io):
[dependencies]
from-user = "0.2"
Note: when using the crate in Rust code, the crate name uses an underscore in
usepaths (hyphens become underscores):
use from_user::{single_line, number};
fn main() {
let s = single_line();
println!("you typed: {}", s);
let n = number();
println!("number: {}", n);
}
If you are testing locally without publishing, add it by path in your binary project's Cargo.toml:
[dependencies]
from-user = { path = "../from-user" }
pub fn single_line() -> StringReads one line from stdin and returns it as a String (including trailing newline if present). It currently prints a short message on read_line errors and returns whatever it could read.
Example:
let s = from_user::single_line();
println!("raw: {:?}", s);
pub fn number() -> i32Reads a line from stdin, validates characters, and attempts to parse the trimmed input into i32.
number() again.Example:
let n = from_user::number();
println!("got int: {}", n);
This crate is simple by design and the current number() implementation has a few rough edges. Below are short, easy-to-understand fixes you can apply as tiny edits.
1. Loop condition is reversed — causes the loop not to run at all.
- while loop_variable > word_length {
+ while loop_variable < word_length {
2. Character check logic is inverted.
- if char_to_check.is_ascii_digit() {
- println!("Please enter a Numerics only");
- return number();
- }
+ if !char_to_check.is_ascii_digit() {
+ println!("Please enter numerics only");
+ return number();
+ }
3. loop_variable never increments → infinite loop.
Add this at the end of the loop body:
+ loop_variable += 1;
chars().nth() can return None (out of bounds)..expect(SRS) which will panic if something goes wrong. The fixes above (loop condition and increment) avoid the out-of-bounds case. If you prefer safer code, use pattern matching on the Option<char>.The crate currently uses a match on parse() and calls number() again on Err(_) — that is fine for simplicity. Be aware that heavy recursion on many bad inputs could grow the call stack; for interactive use this is usually not a problem.
loop to avoid stack growth.From your Cargo.toml:
MIT OR Apache-2.0