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

jnbdz

我正在努力在 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正在声明,但未初始化为值(初始化需要使用 赋值=)。

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章