Does the `rustc` optimizer avoid unnecessary memory allocations in loops?

Considering the following code:

for entry in fs::read_dir(".")? {
    let path = entry?.path();
    if path.is_file() && path.extension() == Some(OsStr::new("txt")) {
        println!("found txt file");
    }
}

will the OsStr::new("txt") allocation happen only once, or should I manually instruct the compiler as follows?

let txt = OsStr::new("txt");
for entry in fs::read_dir(".")? {
    let path = entry?.path();
    if path.is_file() && path.extension() == Some(txt) {
        println!("found txt file");
    }
}

Are there simple ways to inspect number of memory allocations in Rust? I refer to something similar to the Julia's @time macro, or anyway providing similar information.

I found a similar question here, but not exactly what I am looking for.
Thanks

OsStr::new does not allocate: OsStr is a borrowed counterpart to the owned OsString.

I don't know if LLVM is able to elide allocations from loops, but that seems unlikely, so it's usually a good idea to amortize allocations across the whole loop.

I don't know of a specific way to provide memory allocations. There are some hooks in jemalloc for that, but I haven't used them. In general, I just profile with perf, and if allocation functions are a problem, they are very much visible in the flamegraphs.

I did run it in godbolt.org using OsString. (left: inlined in loop; right: allocated before the loop)

and if see line 265 on the left side it says to me that rust don't optimizes the allocation.