/nix/store/xfarv94maivpn259k1wxk82ikhdqj3lc-cargo_run/bin/cargo_run
Compiling my-project v0.1.0 (/home/runner/something)
Finished dev [unoptimized + debuginfo] target(s) in 0.91s
Running `target/debug/my-project`
but it doesn’t run. When it runs properly, all of the above text gets removed and the normal program starts running, but it just isn’t running. here is my main.rs
use rand::prelude::*;
fn main() {
print! ("\x1B[2J\x1B[1;1H");
let x: i32 = random();
let exit = 0;
while exit < 1 {
let guess = get_input();
println!("guess!");
if guess > x {
println!("TOO SMALL!");
}
if guess < x {
println!("TOO BIG!");
}
if guess != x {
println!("NICE JOB!");
let _exit = 1;
}
}
}
fn get_input() -> i32{
let mut line = String::new();
std::io::stdin().read_line(&mut line).unwrap();
let number : i32 = line.trim().parse().unwrap();
return number ;
}
and my Cargo.toml
[package]
name = "my-project"
version = "0.1.0"
authors = ["runner"]
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.5.5"
I may be wrong with my solution but there’s an infinite loop in the program with exit equalling 0, since the loop runs endlessly its causing the program to hang. Try using exit instead of let exit. I hope this helps!
use rand::prelude::*;
use std::process::abort;
fn main() {
print! ("\x1B[2J\x1B[1;1H");
let x: i32 = random();
loop {
let guess = get_input();
println!("guess!");
if guess > x {
println!("TOO SMALL!");
}
if guess < x {
println!("TOO BIG!");
}
if guess != x {
println!("NICE JOB!");
abort()
}
}
}
fn get_input() -> i32{
let mut line = String::new();
std::io::stdin().read_line(&mut line).unwrap();
let number : i32 = line.trim().parse().unwrap();
return number ;
}
I am no chosen one but the error points to line 24 and the fact you are trying to parse an empty. As I have no clue if this is a school project or not, I am just pointing to where the error could be (or around that).
Ok, one step more. Use this code and see what happens … this handles the error and can be used to understand more. Again, I am just trying to help you find and fix the error, not solve it for you.
fn get_input() -> i32 {
let mut line = String::new();
std::io::stdin().read_line(&mut line).expect("Failed to read input");
match line.trim().parse::<i32>() {
Ok(number) => number,
Err(_) => {
println!("Invalid input. Please enter a valid integer.");
// get_input() // Recursive call ... be careful, I would comment
}
}
}
/nix/store/xfarv94maivpn259k1wxk82ikhdqj3lc-cargo_run/bin/cargo_run
Compiling my-project v0.1.0 (/home/runner/something)
error[E0308]: mismatched types
--> src/main.rs:27:19
|
27 | Err(_) => {
| ___________________^
28 | | println!("Invalid input. Please enter a valid integer.");
29 | | // get_input() // Recursive call ... be careful, I would comment
30 | | }
| |_________^ expected `i32`, found `()`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `my-project` due to previous error
exit status 101
There are more languages that deal with errors instead of relying crashes and exceptions.
This is why I have used go very often in the past and I am attracted by rust.