Getting Variable from other function

Viewed 39

I don't know how to get (_s) variable from other function. Maybe there is a way to reformat this script, so it could work or any other way.

fn shapes(data: usize) {
    let romb = "hello";
    let s_array = [romb];
    let mut items_array = -1;

    print!("{}", s_array[data]);
    if items_array == _s { //<-------this _s variable is not in this function, so it doesn't work
    } else {
        println!("error");
    }
    for x in &s_array {
        items_array = items_array + 1;
    }

    print!("{}", items_array);
}

fn main() {
    let mut _s = String::new();
    let _b1 = std::io::stdin()
        .read_line(&mut _s)
        .expect("failed to read line");
    let mut _s = _s.trim_end();

    let _s_int = _s.parse::<usize>().unwrap();

    shapes(_s_int);
}
1 Answers

You need to add another parameter to your shapes function to pass _s. So extend the prototype of your function like:

fn shapes(data: usize, _s: String) {
    //Use _s here
}

to call it like:

shapes(_s_int, _s);
Related