创建临时目录项,在读取DirEntry路径时仍在使用时会被释放。

huangapple go评论104阅读模式
英文:

creates temporary which is freed while still in use when reading path of DirEntry

问题

I'm trying to read all files in a folder, as simple as that. And then filter for mp4 extensions. But I get following error:

创建临时目录项,在读取DirEntry路径时仍在使用时会被释放。

I don't understand, how in the following code, the e.path statement could "create a temporary". (The whole code is obviously in them same "Ok" closure.). Is there some special stuff happening in the path() method? I could not find anything strange in the documentation.

  1. let paths = fs::read_dir(&folder).unwrap();
  2. for path in paths {
  3. match path {
  4. Ok(e) => {
  5. let fullPath = e.path();
  6. let extension = e
  7. .path()
  8. .extension()
  9. .expect("Error getting extension")
  10. .to_str()
  11. .expect("Could not convert extensions to string");
  12. println!("{}", extension)
  13. }
  14. _ => {}
  15. }
  16. }

I tried numerous things and stuff like .path().metadata() does not throw this error. Only when in combination with extension() this seems to be happening? It's really confusing me, how a task this simple becomes so complex.

英文:

I'm trying to read all files in a folder, as simple as that. And then filter for mp4 extensions. But I get following error:

创建临时目录项,在读取DirEntry路径时仍在使用时会被释放。

I don't understand, how in the following code, the e.path statement could "create a temporary". (The whole code is obviously in them same "Ok" closure.). Is there some special stuff happening in the path() method? I could not find anything strange in the documentation.

  1. let paths = fs::read_dir(&folder).unwrap();
  2. for path in paths {
  3. match path {
  4. Ok(e) => {
  5. let fullPath = e.path();
  6. let extension = e
  7. .path()
  8. .extension()
  9. .expect("Error getting extension")
  10. .to_str()
  11. .expect("Could not convert extensions to string");
  12. println!("{}", extension)
  13. }
  14. _ => {}
  15. }

I tried numerous things and stuff like .path().metadata() does not throw this error. Only when in combination with extension() this seems to be happening? It's really confusing me, how a task this simple becomes so complex.

答案1

得分: 1

DirEntry::path() 返回一个 PathBufPathBuf::extension() 返回一个 Option<&amp;OsStr>。正如你所见,OsStr 是从原始的 PathBuf / Path 中借用的。问题是,你试图将扩展名绑定到一个变量,但你没有分配从中借用的 PathBuf 给一个变量。

因此编译器正确地指出 PathBuf 是临时的,并且会在 let extension = ... 语句之后被丢弃,因此 extension 不能继续指向它。

你已经做对了 - 将那个 PathBuf 的另一个副本赋值给 fullPath。所以你可以直接从中借用扩展名。

这里是一个完全独立的解决方案:

  1. use std::fs;
  2. fn main() {
  3. let folder = "./src";
  4. let paths = fs::read_dir(&amp;folder).unwrap();
  5. for path in paths {
  6. if let Ok(e) = path {
  7. let full_path = e.path(); // 这将至少存活和 `extension` 一样长的时间,所以从中借用是可以的。
  8. let extension = full_path
  9. .extension()
  10. .expect("获取扩展名时出错")
  11. .to_str()
  12. .expect("无法将扩展名转换为字符串");
  13. println!("{}", extension)
  14. }
  15. }
  16. }
英文:

DirEntry::path() returns a PathBuf, and PathBuf::extension() returns an Option&lt;&amp;OsStr&gt;. As you can see, the OsStr is borrowed from the original PathBuf / Path. The problem is, you're trying to bind the extension to a variable, but you do not assign the PathBuf that is borrowed from to a variable as well.

So the compiler is correct in saying that the PathBuf is temporary, and will be dropped after your let extension = ... statement, and thus extension can not keep pointing to it.

You're already doing the right thing - assigning another copy of that PathBuf to fullPath. So you can just borrow the extension from it.

Here's a fully self-contained solution:

  1. use std::fs;
  2. fn main() {
  3. let folder = &quot;./src&quot;;
  4. let paths = fs::read_dir(&amp;folder).unwrap();
  5. for path in paths {
  6. if let Ok(e) = path {
  7. let full_path = e.path(); // This will live at least as long as `extension`, so
  8. // borrowing from it is fine.
  9. let extension = full_path
  10. .extension()
  11. .expect(&quot;Error getting extension&quot;)
  12. .to_str()
  13. .expect(&quot;Could not convert extensions to string&quot;);
  14. println!(&quot;{}&quot;, extension)
  15. }
  16. }
  17. }

huangapple
  • 本文由 发表于 2023年5月7日 21:00:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194095.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定