Does CLion possible evaluate a function when debugging Rust code?
Asked Answered
P

2

5

A snip of Rust code:

pub fn main() {
    let a = "hello";
    let b = a.len();
    let c =b;
    println!("len:{}",c)
}

When debugging in CLion, Is it possible to evaluate a function? For example, debug the code step by step, now the code is running to the last line println!... and the current step stops here, by adding the expression a.len() to the watch a variable window, the IDE can't evaluate the a.len(). It says: error: no field named len

CLion can't evaluate an expression on Rust code

Purpose answered 20/1, 2022 at 13:4 Comment(0)
A
3

This is the same reason you can't make conditional breakpoints for Rust code:

Can't create a conditional breakpoint in VSCode-LLDB with Rust

Appellative answered 20/1, 2022 at 17:10 Comment(1)
So what/how do people debug code when working in Rust? I'm new to Rust and not sure how to understand patterns and structures without a functioning debugger. Is there a better IDE/debugger combination that can be used instead?Cholon
M
4

I hope, I'm not too late to answer this, but with both lldb and gdb, Rust debugging capability is currently rather constrained. Expressions that are straightforward work; anything complex is likely to produce issues.

My observations from rust-lldb trying this, are that only a small portion of Rust is understood by the expression parser. There is no support for macros.

Non-used functions are not included in the final binary. For instance, since that method is not included in the binary, you are unable to execute capacity() on the HashMap in the debugger.

Methods must be named as follows: struct value.method(&struct value)

There is no technique that I've discovered to call monomorphized functions on generic structs (like HashMap).

For example, "hello" is a const char [5] including the trailing NUL byte. String constants "..." in lldb expressions are produced as C-style string constants. Therefore, they are not valid functions

Mareld answered 7/9, 2022 at 19:33 Comment(0)
A
3

This is the same reason you can't make conditional breakpoints for Rust code:

Can't create a conditional breakpoint in VSCode-LLDB with Rust

Appellative answered 20/1, 2022 at 17:10 Comment(1)
So what/how do people debug code when working in Rust? I'm new to Rust and not sure how to understand patterns and structures without a functioning debugger. Is there a better IDE/debugger combination that can be used instead?Cholon

© 2022 - 2024 — McMap. All rights reserved.