无法在 Rust 的 while 循环中填充字符串数组

Unable to populate an array of strings in a while loop in Rust

我正在努力用 Rust 填充数组。

这是我的代码:

use std::convert::TryInto;
use rand::rngs::mock::StepRng;

fn main() {
    println!("Blackjack!");

        let mut arr_num: [String; 10];
        let mut i = 0;
        let mut n = 2;

    // Loop while `n` is less than 101
    while n < 11 {
        arr_num[i] = n.to_string();
        // Increment counter
        n += 1;
                i += 1;
    }

    let arr: [i32; 5] = (1..=5).collect::<Vec<_>>()
            .try_into().expect("wrong size iterator");
    let mut first = StepRng::new(2, 11);
    //let mut first: [&str; 10] = ["2"; "11"];
    let mut ranks: [&str; 4] = ["JACK", "QUEEN", "KING", "ACE"];
    let mut suits: [&str; 4] = ["SPADE", "HEART", "DIAMOND", "CLUB"];

    println!("arr_num is {:?}", arr_num);
    println!("arr is {:?}", arr);
    println!("Ranks is {:?}", first);
    println!("Ranks is {:?}", ranks);
    println!("Suits is {:?}", suits);
}

我收到这个错误:

error[E0381]: use of possibly-uninitialized variable: `arr_num`
  --> src/main.rs:18:3
   |
18 |         arr_num[i] = n.to_string();
   |         ^^^^^^^^^^ use of possibly-uninitialized `arr_num`

如果我尝试这样做:let mut arr_num: [&str; 10];

我收到这个错误:

error[E0308]: mismatched types
  --> src/main.rs:18:16
   |
18 |         arr_num[i] = n.to_string();
   |         ----------   ^^^^^^^^^^^^^
   |         |            |
   |         |            expected `&str`, found struct `String`
   |         |            help: consider borrowing here: `&n.to_string()`
   |         expected due to the type of this binding

error[E0283]: type annotations needed
  --> src/main.rs:18:18
   |
18 |         arr_num[i] = n.to_string();
   |                      --^^^^^^^^^--
   |                      | |
   |                      | cannot infer type for type `{integer}`
   |                      this method call resolves to `String`
   |
   = note: multiple `impl`s satisfying `{integer}: ToString` found in the `alloc` crate:
           - impl ToString for i8;
           - impl ToString for u8;

我也试过使用 & 就像错误说的那样:

error[E0381]: use of possibly-uninitialized variable: `arr_num`
  --> src/main.rs:18:3
   |
18 |         arr_num[i] = &n.to_string();
   |         ^^^^^^^^^^ use of possibly-uninitialized `arr_num`

error[E0716]: temporary value dropped while borrowed
  --> src/main.rs:18:17
   |
18 |         arr_num[i] = &n.to_string();
   |         ----------    ^^^^^^^^^^^^^- temporary value is freed at the end of this statement
   |         |             |
   |         |             creates a temporary which is freed while still in use
   |         borrow later used here
   |
   = note: consider using a `let` binding to create a longer lived value

我认为我正在尝试做的事情非常简单。那么如何在 Rust 中填充 str 数组?

该循环实际上与您的错误没有任何关系,它的发生方式与此相同:

fn main() {
    let mut arr_num: [String; 10];

    arr_num[0] = "hi".to_string();
    
    println!("{arr_num:?}")
}

您的arr_num正在声明,但未初始化一个值(初始化需要使用=).

看起来你并不关心初始值是什么,因为你是在循环中分配它,所以你应该将它初始化为默认值(空数组 Strings) :

fn main() {
    let mut arr_num: [String; 10] = Default::default();

    arr_num[0] = "hi".to_string();
    
    println!("{arr_num:?}")
}