Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2019
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. extern crate walkdir;
  2. use walkdir::WalkDir;
  3. use std::path::Path;
  4. use std::ffi::OsStr;
  5.  
  6. fn get_extension_from_filename(filename: &str) -> Option<&str> {
  7. Path::new(filename)
  8. .extension()
  9. .and_then(OsStr::to_str)
  10. }
  11.  
  12. fn main() {
  13. let mut cpp_files = Vec::new();
  14.  
  15. for e in WalkDir::new(".").into_iter().filter_map(|e| e.ok()) {
  16. if e.metadata().unwrap().is_file() {
  17. let p = e.path().display().to_string().as_str();
  18.  
  19. if get_extension_from_filename(p) == Some("cpp") {
  20. cpp_files.push(p);
  21. }
  22. }
  23. }
  24. }
  25.  
  26. Error:
  27. error[E0716]: temporary value dropped while borrowed
  28. --> src\main.rs:17:21
  29. |
  30. 17 | let p = e.path().display().to_string().as_str();
  31. | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
  32. | |
  33. | creates a temporary which is freed while still in use
  34. 18 |
  35. 19 | if get_extension_from_filename(p) == Some("cpp") {
  36. | - borrow used here, in later iteration of loop
  37. |
  38. = note: consider using a `let` binding to create a longer lived value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement