title: How to check if file is executable using bitwise operations in Rust?
body: <p>In many programming languages one can find if file is executable or not,  by doing a <a href="https://en.wikipedia.org/wiki/Bitwise_operation" rel="nofollow">bitwise and</a> between the <a href="https://en.wikipedia.org/wiki/File_system_permissions" rel="nofollow">mode bits</a> and <code>0111</code>, something like:</p>
  
  <pre><code>is_exec = (mode &amp; 0111) != 0 
  </code></pre>
  
  <p>Where <code>mode</code> represents the mode bits with an octal value something like <code>100755</code>, or <code>755</code>.</p>
  
  <p>Command line example:</p>
  
  <pre><code>perl -e 'printf "%o\n", (stat "file")[2] &amp; 0111'
  </code></pre>
  
  <p>I am trying to follow this approach using Rust, but can't make it work, this is the code I am using:</p>
  
  <pre><code>fn print_dir(path: &amp;PathBuf) {                                                                                                                                            
      let files = match fs::read_dir(&amp;path) {                                                                                                                               
          Err(f) =&gt; {                                                                                                                                                       
              println!("{}", f);                                                                                                                                            
              return;                                                                                                                                                       
          }                                                                                                                                                                 
          Ok(f) =&gt; f                                                                                                                                                        
      };                                                                                                                                                                    
  
      for f in files {                                                                                                                                                      
          let file = f.unwrap();                                                                                                                                            
          let mode = file.metadata().unwrap().permissions().mode();                                                                                                         
          println!("path: {} {:o} - {} {:0}", file.path().display(), mode, mode, mode &amp; 0111);                                                                              
      }                                                                                                                                                                     
  } 
  </code></pre>
  
  <p>The <code>mode &amp; 0111</code> is not behaving like expected as it always returns positives. I tried similar code in Go, and it was very straight forward:</p>
  
  <pre><code>var is_exec bool
  if m := f.Mode(); !m.IsDir() &amp;&amp; m&amp;0111 != 0 {
      is_exec = true
  }
  </code></pre>
  
  <p>Any idea of how to do this with Rust?</p>