小编ask*_*sky的帖子

如何在 Rust 函数中返回一个数组

我想在 Rust 中创建一个函数,它生成一个具有随机值的 x 大小的数组。我想问一下如何在 Rust 函数中返回一个数组,如果你能检查我的其余代码是否正常,那就太好了。我很抱歉我的基本问题,但我是初学者。

use rand::prelude::*;

fn generateArray(howManyValues: u32)->[f64]
{
    let mut rng = rand::thread_rng();
    let array: [f64, howManyValues];
    for i in 0..howManyValues
    {
        array[i] = rng.gen()*100;
    }

    println!("{:?}", array);
    return array;
}

fn main() {
    generateArray(10);
}
Run Code Online (Sandbox Code Playgroud)

arrays rust

8
推荐指数
1
解决办法
8932
查看次数

线程“main”在 Rust 中溢出了它的堆栈

我正在尝试学习 Rust(我来自 Java),但遇到了一些问题。我正在构建一个简单的程序,它是连接池的基础。当我运行它时,我收到运行时错误thread 'main' has overflowed its stack,我不明白为什么。

这是 main.rs

use hello_rust::concurrent_bag;
use hello_rust::concurrent_bag::BagEntry;

fn main() {
    let my_bagentry = BagEntry::new(String::from("ciao"));
    //println!("{}", my_bagentry.value());
    let mut contVec : Vec<BagEntry<String>>=vec![];
    contVec.push(my_bagentry);
    println!("state ={}", contVec[0]);
    println!("state ={}", contVec[0].value());
    let mut my_bag: concurrent_bag::ConcurrentBag<String> =concurrent_bag::ConcurrentBag::new();
    my_bag.addEntry(String::from("ciao Entry"));
    let result = my_bag.borrowEntry();
    if result.is_some() {
    println!("{}", result.unwrap().value());
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 lib.rs

pub mod concurrent_bag {
    use crate::concurrent_bag::BagEntryState::UNUSED;

    pub enum BagEntryState {
        UNUSED, USED, REMOVED
    }

    impl fmt::Display for BagEntryState {
        fn fmt(&self, f: &mut …
Run Code Online (Sandbox Code Playgroud)

rust

1
推荐指数
1
解决办法
994
查看次数

标签 统计

rust ×2

arrays ×1