简体   繁体   English

为什么此Rust 2018代码使用`cargo build`编译但不使用rustc编译?

[英]Why does this Rust 2018 code compile with `cargo build` but not using rustc?

When compiling this snippet below using cargo build, the borrow checker seems fine but when using rustc I get the error 在使用cargo build编译以下代码段时,借用检查器似乎很好,但是在使用rustc时出现错误

error[E0502]: cannot borrow `char_counts` as mutable because it is also borrowed as immutable
  --> src/lib.rs:14:17
   |
10 |         let count = char_counts.get(&char);
   |                     ----------- immutable borrow occurs here
...
14 |                 char_counts.insert(char, rem);
   |                 ^^^^^^^^^^^ mutable borrow occurs here
...
19 |     }
   |     - immutable borrow ends here

Any ideas why this happens? 任何想法为什么会发生这种情况?

use std::collections::HashMap;

pub fn anagram(word: &str, another_word: &str) -> i32 {
    let mut char_counts = HashMap::new();
    for char in word.chars() {
        let count = char_counts.entry(char).or_insert(0);
        *count += 1;
    }
    for char in another_word.chars() {
        let count = char_counts.get(&char);
        if let Some(val) = count {
            let rem = val - 1;
            if rem > 0 {
                char_counts.insert(char, rem);
            } else {
                char_counts.remove(&char);
            }
        }
    }
    println!("{:?}", char_counts);
    return char_counts.keys().len() as i32;
}

cargo --version and rustc --version commands both output 1.33 cargo --versionrustc --version命令都输出1.33

This function will compile fine if you have non-lexical lifetimes enabled and won't compile without them. 如果启用了非词性生存期 ,则此函数可以正常编译,并且如果没有它们,就无法编译。 The 2018 edition enables them by default. 默认情况下,2018版启用它们。 Perhaps you have edition = "2018" in your Cargo.toml , but are not passing it as parameter when using rustc directly? 也许您在Cargo.toml具有edition = "2018" ,但是在直接使用rustc时没有将其作为参数传递吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Rust会先用`cargo build --release`再加上`cargo run`再次编译我的项目? - Why does Rust compile my project again with `cargo build --release` followed by `cargo run`? 如何仅使用 cargo 或 rustc 为自定义目标编译 Rust 核心板条箱? - How can I compile the Rust core crate for a custom target using only cargo or rustc? 当rustc启用musc时,未能使用货物建立rust-libc - failure to build rust-libc using cargo when rustc is musl-enabled 无法运行“rustc”错误。 (生锈货物构建问题) - Failed to run `rustc` error. (Rust cargo build issue) 如何仅使用rustc而不使用商品链接动态Rust库? - How to link a dynamic Rust library using only rustc and not cargo? 如果“货物建造”比直接运行rustc慢,为什么要使用Cargo? - Why should I use Cargo if “cargo build” is slower than running rustc directly? 如何解决为什么货物/ rustc链接锈标准库符号,即使使用no_std? - How to troubleshoot why cargo/rustc links in rust standard library symbols even when no_std is used? 在Xcode中使用NSTask在AppKit应用程序中运行时,Rust 1.4.0(或更高版本)货物/铁锈崩溃 - Rust 1.4.0 (or later) cargo/rustc crashes when run in an AppKit app using NSTask in Xcode 使用Rust Cargo进行构建的不同目标名称 - Different target names using Rust Cargo for build 为什么`cargo build`没有在我的代码中显示所有错误? - Why does `cargo build` not show all errors in my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM