fn main() {
println!("This is a line");
pause();
println!("This is another line");
}
fn pause() {
println!("Pausing! Press enter to continue...");
let mut buffer = String::new();
std::io::stdin()
.read_line(&mut buffer)
.expect("Failed to read line");
}
How can i print the file name, the line number and pause the program execution in line 4 where i called the pause() function ?
I want the pause function to pause the program execution and print something like this: [src/main.rs:4] Pausing! Press enter to continue...
Isn't there something in the standard library to do that ? All this code just to pause the program and type a message that has some info !!
There are 2 problems with this:
fn main() {
println!("This is a line");
pause();
println!("This is another line");
}
fn pause() {
dbg!("Pausing! Press enter to continue...");
let mut buffer = String::new();
std::io::stdin()
.read_line(&mut buffer)
.expect("Failed to read line");
}
Output:
This is a line
[src/bin/test.rs:10] "Pausing! Press enter to continue..." = "Pausing! Press enter to continue..."
This is another line
The file name and the line number is where the dbg!() where called (line 10), not where the pause() function called. I want the line where the pause() been called.
The output is super ugly, instead of this:
[src/bin/test.rs:10] "Pausing! Press enter to continue..." = "Pausing! Press enter to continue..."
i want this: [src/bin/test.rs:4] Pausing! Press enter to continue...
[src/main.rs:33] This is a line
[src/main.rs:35] Pausing! Press enter to continue...
[src/main.rs:37] This is another line
[src/main.rs:39] Pausing! Press enter to continue...
(notice that the line number in the log messages for the pauses is now equal to the lines where we used pause!)
Nice, I didn't know about file!, line! and column! macros. This is even better as it avoids the dependency on the log and env_logger crates in your case.
Can't help you with that, unfortunately. There's a category "Editors and IDEs" here on URLO, maybe open a new topic there?
fn main() {
println!("This is a line");
pause();
println!("This is another line");
}
#[track_caller]
fn pause() {
let caller = std::panic::Location::caller();
println!(
"[{}: {}] Pausing! Press enter to continue...",
caller.file(),
caller.line()
);
let mut buffer = String::new();
std::io::stdin()
.read_line(&mut buffer)
.expect("Failed to read line");
}
You need the #[track_caller] at the definition of pub fn pause() to make Rust track the caller's location, and not the line on which you call let caller = std::panic::Location::caller();
See Rust Playground - the version of fn pause I've called tracked_pause is the one you want, and reproduced below:
// NB: The `track_caller` attribute is necessary here - without it,
// caller.file() and caller.line()` are going to be in `fn pause()`, not
// the caller's location.
#[track_caller]
pub fn pause() {
let caller = std::panic::Location::caller();
println!(
"[{}:{}] Pausing! Press enter to continue...",
caller.file(),
caller.line(),
);
let mut buffer = String::new();
std::io::stdin()
.read_line(&mut buffer)
.expect("Failed to read line");
}