Hugal31 / yara-rust

Rust bindings for VirusTotal/Yara
Apache License 2.0
73 stars 29 forks source link

"Get Started" tutorial can't work. #81

Closed ghost closed 2 years ago

ghost commented 2 years ago

source code (the same to tutorial)

use yara::Compiler;

const RULES: &str = r#"
    rule contains_rust {
      strings:
        $rust = "rust" nocase
      condition:
        $rust
    }
"#;

fn main() {
    let compiler = Compiler::new().unwrap();
    compiler.add_rules_str(RULES)
        .expect("Should have parsed rule");
    let rules = compiler.compile_rules()
        .expect("Should have compiled rules");
    let results = rules.scan_mem("I love Rust!".as_bytes(), 5)
        .expect("Should have scanned");
    assert!(results.iter().any(|r| r.identifier == "contains_rust"));
}

error information

error[E0382]: use of moved value: `compiler`
   --> src/main.rs:16:17
    |
13  |     let compiler = Compiler::new().unwrap();
    |         -------- move occurs because `compiler` has type `Compiler`, which does not implement the `Copy` trait
14  |     compiler.add_rules_str(RULES)
    |              -------------------- `compiler` moved due to this method call
15  |         .expect("Should have parsed rule");
16  |     let rules = compiler.compile_rules()
    |                 ^^^^^^^^ value used here after move
    |
note: this function takes ownership of the receiver `self`, which moves `compiler`
   --> /home/developer/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/yara-0.15.0/src/compiler.rs:103:26
    |
103 |     pub fn add_rules_str(self, rule: &str) -> Result<Compiler, Error> {
    |                          ^^^^

Environment

OS: Ubuntu 22.04 Toolchain: stable-x86_64-unknown-linux-gnu (default) C Compiler: both GCC and Clang on my pc

Hugal31 commented 2 years ago

Oups, sorry about that. The compiler methods indeed move it because the compiler is made unusable when it fails to compile a rule. I fixed the example in the README and also added in the examples directory.

ghost commented 2 years ago

Oups, sorry about that. The compiler methods indeed move it because the compiler is made unusable when it fails to compile a rule. I fixed the example in the README and also added in the examples directory.

I greatly appreciate it!