# Prototyping in Rust
- Early in a Rust project, it’s okay to **`clone()` and `unwrap()`** liberally – this allows rapid prototyping **without fighting the borrow checker**
- Even with `clone()`, Rust is **still much faster** than Python and often faster than Go
- **Once the structure/design is stable**, optimize by:
- Using references (`&T`) instead of cloning
- Propagating errors with `Result<>` instead of panicking (`unwrap()`)
- If further optimized, Rust can be **blazing fast**!
Example:
```rust
use std::fs;
fn read_and_double_prototype() -> i32 {
// Convert filename to a String and clone it unnecessarily
let filename = "number.txt".to_string();
// Read file content, panicking on any error
let content = fs::read_to_string(filename.clone()).unwrap();
// Parse the content to an integer, unwrapping the result
let number: i32 = content.trim().parse().unwrap();
number * 2
}
fn main() {
println!("Double is: {}", read_and_double_prototype());
}
```
More optimized example:
```rust
use std::error::Error;
use std::fs;
fn read_and_double_optimized() -> Result<i32, Box<dyn Error>> {
// Use a string literal for the filename, avoiding an unnecessary clone
let filename = "number.txt";
// Read the file, using the `?` operator to propagate errors
let content = fs::read_to_string(filename)?;
// Parse the content into an integer, again propagating errors
let number: i32 = content.trim().parse()?;
Ok(number * 2)
}
fn main() -> Result<(), Box<dyn Error>> {
match read_and_double_optimized() {
Ok(doubled) => println!("Double is: {}", doubled),
Err(err) => println!("Error: {}", err),
}
Ok(())
}
```
## Resources
- https://corrode.dev/blog/prototyping/
- [Andre Bogus - Easy Mode Rust](https://www.youtube.com/watch?v=33FG6O3qejM)