cs50_rust
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.
What is this?
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.
Installation
You have three ways to use this library:
Option 1: Install from crates.io (Easiest)
Add this to your Cargo.toml:
[]
= "1.0.0"
Or use cargo add:
Option 2: Use directly from GitHub
If you want the latest development version, add this to your Cargo.toml:
[]
= { = "https://github.com/richarddalves/cs50_rust" }
Option 3: Download and use locally
Clone the repository and use it as a local dependency:
Then in your project's Cargo.toml:
[]
= { = "../path/to/cs50_rust" }
Usage
use ;
Available Functions
Basic CS50 Functions
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 |
Additional Rust-Specific Functions
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 |
-(2127) to 2127 - 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 |
A Note on Float Types
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.
Boolean Input
The get_bool() function accepts several formats:
true,falseyes,noy,n1,0
All inputs are case-insensitive.
Examples
Simple Calculator
use ;
Grade Average
use get_float;
Running the Examples
Clone the repository and run:
How It Works
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.
Building and Testing
# Build the library
# Run tests
# Generate documentation
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.
Disclaimer
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.
Contributing
Contributions are welcome. Please see CONTRIBUTING.md for guidelines.
Acknowledgments
- Inspired by CS50's C library
- Thanks to the Rust community for excellent documentation and tools
- Special thanks to Harvard's CS50 course for making computer science education accessible
Related Resources
Made with ❤️ for CS50 students learning Rust