![](/style/images/good.png)
![](/style/images/bad.png)
Trace caller's source location in the stable Rust
source link: https://blog.knoldus.com/trace-callers-source-location-in-the-stable-rust/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Trace caller's source location in the stable Rust
Reading Time: < 1 minute
Hello folks, In this blog, I will be introducing a feature to track source location of caller of a function.
In one of the Rust projects, I came across a scenario, where I had to trace source location of caller of a function. Rust also has this feature, but it is not available for stable release. So I published a crate trace_caller and introduced trace
, a procedural attribute macro to track the source location. This crate will work in stable release of Rust.
This library uses backtrace
library in backend for acquiring backtraces at runtime.
How to use trace_caller:-
1) Add dependencies in Cargo.toml
[dependencies]
trace_caller = "0.2.0"
2) Now use in your code
use trace_caller::trace;
#[trace]
fn add(x: u32, y: u32) -> u32 {
x + y
}
fn main() {
let result = add(3, 4);
println!("Result: {}", result);
}
3) Run cargo.run
Called from "src/main.rs" at line 9
Result: 7
You can find source code here https://github.com/ayushmishra2005/trace_caller. Please feel free to raise pull request.
Thank you for reading the blog!!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK