I'm trying to use VSCode for interactive debugging of a Rust code. When I debug a code like following I can see values of the x and y variables only in the first block but not in the second. Within the second block the x and y variables are still shown as Variable is optimized away and not available.
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read line");
let mut iter = input.split_whitespace();
let mut desk = {
let x: i32 = iter.next().unwrap().parse().unwrap();
let y: i32 = iter.next().unwrap().parse().unwrap();
knights::Desk::new(x, y)
};
let start;
let res = {
let x: i32 = iter.next().unwrap().parse().unwrap();
let y: i32 = iter.next().unwrap().parse().unwrap();
start = Instant::now();
desk.find_first(x, y)
};
Second, it's probably not exactly a bug in either. Compilers sometimes notice some variables are not needed (they can rewrite the code without them) and simply throw them away. Then you can't see their value. It's unfortunate, but it usually happens only in release (optimised) builds.
I don't know how to help, but I can relate: I'm also trying to debug Rust code in VSCode, and the variables window lists almost all variables as optimized away at all times. Surely that can't be right even if I'm not running the debug build I think I am.
you can use cargo build at the terminal at first, then use vscode for interactive debugging for Rust code. such Variable is optimized away and not available will not shown.