| Crates.io | cs50_rust |
| lib.rs | cs50_rust |
| version | 1.0.0 |
| created_at | 2025-08-05 23:07:59.767001+00 |
| updated_at | 2025-08-05 23:07:59.767001+00 |
| description | Simple input functions for Rust, inspired by CS50's library |
| homepage | |
| repository | https://github.com/richarddalves/cs50_rust |
| max_upload_size | |
| id | 1783166 |
| size | 31,388 |
Simple input functions for Rust, inspired by CS50's C library.
Important: This is an unofficial project and is not affiliated with Harvard University or the CS50 course.
If you've taken CS50, you know functions like get_int() and get_string() that make getting user input straightforward. This library brings similar functionality to Rust, helping beginners focus on learning programming concepts rather than fighting with input handling.
The functions in this library will automatically retry when given invalid input, just like their CS50 counterparts. If you ask for an integer and the user types "hello", it will ask again until it gets a valid number.
You have three ways to use this library:
Add this to your Cargo.toml:
[dependencies]
cs50_rust = "1.0.0"
Or use cargo add:
cargo add cs50_rust
If you want the latest development version, add this to your Cargo.toml:
[dependencies]
cs50_rust = { git = "https://github.com/richarddalves/cs50_rust" }
Clone the repository and use it as a local dependency:
git clone https://github.com/richarddalves/cs50_rust.git
Then in your project's Cargo.toml:
[dependencies]
cs50_rust = { path = "../path/to/cs50_rust" }
use cs50_rust::{get_int, get_string, get_float};
fn main() {
let name = get_string("What's your name? ");
let age = get_int("What's your age? ");
let height = get_float("Height in meters: ");
println!("Hello {}, you are {} years old and {:.2}m tall",
name, age, height);
}
These are the main functions that match CS50's original library:
| Function | Returns | Description |
|---|---|---|
get_string(prompt) |
String |
Gets text input from the user |
get_int(prompt) |
i32 |
Gets a 32-bit integer |
get_long(prompt) |
i64 |
Gets a 64-bit integer |
get_float(prompt) |
f32 |
Gets a 32-bit float |
get_double(prompt) |
f64 |
Gets a 64-bit float |
get_char(prompt) |
char |
Gets a single character |
get_bool(prompt) |
bool |
Gets a yes/no or true/false value |
The library also provides functions for Rust's specific numeric types:
| Function | Returns | Range |
|---|---|---|
| Signed integers | ||
get_i8(prompt) |
i8 |
-128 to 127 |
get_i16(prompt) |
i16 |
-32,768 to 32,767 |
get_i32(prompt) |
i32 |
-2,147,483,648 to 2,147,483,647 |
get_i64(prompt) |
i64 |
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
get_i128(prompt) |
i128 |
-(2^127) to 2^127 - 1 |
get_isize(prompt) |
isize |
Platform-dependent (usually same as i32 or i64) |
| Unsigned integers | ||
get_u8(prompt) |
u8 |
0 to 255 |
get_u16(prompt) |
u16 |
0 to 65,535 |
get_u32(prompt) |
u32 |
0 to 4,294,967,295 |
get_u64(prompt) |
u64 |
0 to 18,446,744,073,709,551,615 |
get_u128(prompt) |
u128 |
0 to 2^128 - 1 |
get_usize(prompt) |
usize |
Platform-dependent (usually same as u32 or u64) |
| Floating point | ||
get_f32(prompt) |
f32 |
32-bit floating point |
get_f64(prompt) |
f64 |
64-bit floating point |
In the original CS50 library, get_float() returns a 32-bit float and get_double() returns a 64-bit double. We maintain this distinction for compatibility. However, be aware that in Rust, f64 is generally preferred over f32 for most applications unless you have specific memory or performance constraints.
The get_bool() function accepts several formats:
true, falseyes, noy, n1, 0All inputs are case-insensitive.
use cs50_rust::{get_int, get_char};
fn main() {
let x = get_int("First number: ");
let op = get_char("Operator (+, -, *, /): ");
let y = get_int("Second number: ");
let result = match op {
'+' => x + y,
'-' => x - y,
'*' => x * y,
'/' => x / y,
_ => {
println!("Invalid operator");
return;
}
};
println!("{} {} {} = {}", x, op, y, result);
}
use cs50_rust::get_float;
fn main() {
let mut sum = 0.0;
let count = 3;
for i in 1..=count {
let grade = get_float(&format!("Grade {}: ", i));
sum += grade;
}
println!("Average: {:.2}", sum / count as f32);
}
Clone the repository and run:
cargo run --example basic_usage
Each function displays the prompt and waits for user input. If the input can't be parsed into the requested type, the prompt is shown again. This continues until valid input is received. This behavior matches CS50's library, where functions never fail but keep asking until they get valid input.
This approach is great for learning but might not be suitable for production code where you'd want proper error handling.
# Build the library
cargo build
# Run tests
cargo test
# Generate documentation
cargo doc --open
Licensed under the Apache License, Version 2.0. See LICENSE for details.
This project is not affiliated with, endorsed by, or sponsored by Harvard University, CS50, or any of their partners. CS50 is a registered trademark of Harvard University.
Contributions are welcome. Please see CONTRIBUTING.md for guidelines.
Made with ❤️ for CS50 students learning Rust