user18240098
user18240098

Reputation:

How to get variable changes outside of nesting in rust?

Hey so I'm really new to rust and programming at large and I'm having some issues I haven't encountered before. I'm attempting to return the results of user input variables to the screen outside of nesting but I haven't seemed to have any luck.

use std::{thread, time::Duration};

fn main(){
    let mut modifer = 0;
    let mut ModValue=0;
    let mut ModReason = "";

    let Y = "Y";
    let y = "y";
    let N = "N";
    let n = "n";

    print!("{esc}c", esc = 27 as char);
    let mut ModYN = String::new();
    println!("Are there any modifiers (Y/N)");
    std::io::stdin().read_line(&mut ModYN).unwrap();
    let ModYN = ModYN.trim();

    if ModYN == y || ModYN == Y{
        ModValue = 1;
        print!("{esc}c", esc = 27 as char);
        let mut modifer:f32=0.0;
        let mut input = String::new();
    
        //I'm attempting to get the input from the user to define a modifer reason and value
        println!("Please enter the modifer: ");
        std::io::stdin().read_line(&mut input).expect("Not a valid string");
        modifer = input.trim().parse().expect("Not a valid number");

        let mut ModReason = String::new();
        println!("Whats the modifer reason: ");
        std::io::stdin().read_line(&mut ModReason).unwrap();
        let ModReason = ModReason.trim();
    }
    //Then print those results to the screen outside of nesting
    print!("{esc}c", esc = 27 as char);
    println!("-Modifer-");
    println!("{}",&mut modifer);
    println!("-Modifer Reason-");
    println!("{}",&mut ModReason);
    thread::sleep(Duration::from_secs(5));
}

I've attempted a variety of things; from assigning it as a mutable or borrowing the variable itself to even extending the nesting past the print results. But I've hit a brick wall and could really use some help.

Upvotes: 3

Views: 317

Answers (1)

jthulhu
jthulhu

Reputation: 8668

There are several flaws in your code, and since your question in only about one, I'll explain only about that one. This means that the code below will not compile. But it will get you closer to the solution.

Your problem, I think, comes from the fact that you don't know the difference between let var = value and var = value. These are very different statements: the second one assigns value to var, while the first one creates a new variable (shadowing a variable with the same name, if there was one), then assigns to it. It is the same as

let var;
var = value;

So when, in the loop, you create a new variable ModReason, you are not modifying the one outside the loop: you are effectively creating a new variable, just to destroy it at the end of the if statement (without using it). So the solution is to not create a new variable each time:

use std::{thread, time::Duration};

fn main(){
    let mut modifer = 0;
    let mut ModValue=0;
    let mut ModReason = "";

    let Y = "Y";
    let y = "y";
    let N = "N";
    let n = "n";

    print!("{esc}c", esc = 27 as char);
    let mut ModYN = String::new();
    println!("Are there any modifiers (Y/N)");
    std::io::stdin().read_line(&mut ModYN).unwrap();
    let ModYN = ModYN.trim();

    if ModYN == y || ModYN == Y{
        ModValue = 1;
        print!("{esc}c", esc = 27 as char);
        let mut modifer:f32=0.0;
        let mut input = String::new();
    
        //I'm attempting to get the input from the user to define a modifer reason and value
        println!("Please enter the modifer: ");
        std::io::stdin().read_line(&mut input).expect("Not a valid string");
        modifer = input.trim().parse().expect("Not a valid number");

        ModReason = String::new();
        println!("Whats the modifer reason: ");
        std::io::stdin().read_line(&mut ModReason).unwrap();
        ModReason = ModReason.trim();
    }
    //Then print those results to the screen outside of nesting
    print!("{esc}c", esc = 27 as char);
    println!("-Modifer-");
    println!("{}",&mut modifer);
    println!("-Modifer Reason-");
    println!("{}",&mut ModReason);
    thread::sleep(Duration::from_secs(5));
}

Rust would have warned you (because, in this way, you never assign to ModReason outside of the loop) if you didn't print ModReason by passing a mutable borrow (which is absolutely useless: just go with println!("{}", ModReason);).

Also, it is very likely that Rust has warned you about several variables that are not used: if you read these warnings, you can understand that you are doing something wrong.

Upvotes: 2

Related Questions