rust - 线程 '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());
}
}
这是 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());
似乎运作良好。另一个让我困惑的问题是,如果我&self
在 borrowEntry 中使用,我应该使用它*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,
}
}
}
推荐阅读
- php - 从过滤器类别中删除“全部”并设置另一个 jquery
- javascript - 数据表滚动条有时会消失
- python - 如何使用python将一系列HSV值的所有像素设置为openCV中的另一个值?
- ruby-on-rails - redirect_to new_user_session_path 不显示 Flash 消息
- javascript - 离子验证突出显示不适用于离子选择
- android - 删除部分 ImageSpan 时删除整个 ImageSpan
- ssl - TLS 记录协议如何重组接收到的数据?
- r - 在 bookdown 的 pdf_book before_body 中运行 knitr 代码块
- java - Eclipse 光子丢失 java 文件
- javascript - 鼠标右键单击IE11崩溃