gpt4 book ai didi

rust - 线程 'main' 在 Rust 中溢出了它的堆栈

转载 作者:行者123 更新时间:2023-12-03 11:32:19 24 4
gpt4 key购买 nike

我正在尝试学习 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());
}
}

这里是 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 fmt::Formatter) -> fmt::Result {
match *self {
BagEntryState::UNUSED => write!(f, "UNUSED"),
BagEntryState::USED => write!(f, "USED"),
BagEntryState::REMOVED => write!(f, "REMOVED"),
}
}
}

impl PartialEq for BagEntryState {
fn eq(&self, other: &Self) -> bool {
self == other
}
}

pub struct BagEntry< T: std::cmp::PartialEq + fmt::Display> {
state : BagEntryState,
value: T,
}

impl<'a, T: std::cmp::PartialEq + fmt::Display> BagEntry<T> {
pub fn new(value: T) -> BagEntry< T> {
BagEntry {
value,
state: UNUSED,
}
}

pub fn value(&self)->&T {
&self.value
}
pub fn state(&self)->&BagEntryState {
&self.state
}
}

impl<'a, T: std::cmp::PartialEq + fmt::Display> PartialEq for BagEntry<T> {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}

impl<T: std::cmp::PartialEq + fmt::Display> fmt::Display for BagEntry<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value)
}
}

use std::sync::Arc;
use core::fmt;

pub struct ConcurrentBag<T: std::cmp::PartialEq + fmt::Display> {
entry_list:Vec<BagEntry<T>>,
}

impl<'a, T: std::cmp::PartialEq + fmt::Display> ConcurrentBag<T> {
pub fn new() -> ConcurrentBag<T> {
ConcurrentBag {
entry_list: vec![],
}
}

pub fn borrowEntry(&self) -> Option<BagEntry<T>> {
println!("borrow vc size {}", self.entry_list.len());
println!("value ={}", (self).entry_list[0].value());
println!("state ={}", (self).entry_list[0].state());
if (self).entry_list[0].state()==&UNUSED {

}
let result:Option<BagEntry<T>> =None;
result
}

pub fn addEntry(&mut self, value: T) {
let my_bagentry = BagEntry::new(value);
(*self).entry_list.push(my_bagentry);
println!("addEntry vc size {}", self.entry_list.len())
}

pub fn removeEntry(&mut self, value: T) {
let my_bagentry = BagEntry::new(value);
let index =(*self).entry_list.iter().position(|x| *x == my_bagentry).unwrap();
self.entry_list.remove(index);
}
}
}

有问题的行是

if (self).entry_list[0].state()==&UNUSED

我无法理解为什么自那行

println!("state ={}", (self).entry_list[0].state());

看起来效果不错。另一个让我困惑的问题是,如果我在 borrowEntry 中使用 &self 我应该使用 *self 来访问 entry_list 但是程序编译并运行没有错误。

最佳答案

这段代码不会永远重复吗?

impl PartialEq for BagEntryState {
fn eq(&self, other: &Self) -> bool {
self == other
}
}

我们实现 PartialEq 以便可以使用 ==!= 运算符比较类型的实例。因此,在其中使用 self == other 没有意义。

您可以只导出 PartialEq

#[derive(PartialEq)]
pub enum BagEntryState {
UNUSED,
USED,
REMOVED,
}

或者,如果您想手动实现它,您可以这样做。

impl PartialEq for BagEntryState {
fn eq(&self, other: &BagEntryState) -> bool {
match (self, other) {
(&BagEntryState::UNUSED, &BagEntryState::UNUSED)
| (&BagEntryState::USED, &BagEntryState::USED)
| (&BagEntryState::REMOVED, &BagEntryState::REMOVED) => true,
_ => false,
}
}
}

关于rust - 线程 'main' 在 Rust 中溢出了它的堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60135847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com