我有以下代码从文件中读取:
let mut buf: Box<[u8]> = Box::new([0; 1024 * 1024]);
while let Ok(n) = f.read(&mut buf) {
if n > 0 {
resp.send_data(&buf[0..n]);
} else {
break;
}
}
但它导致:
fatal runtime error: stack overflow
我在OS X 10.11上使用Rust 1.12.0。
答案 0 :(得分:5)
正如Matthieu所说,由于初始堆栈分配,Box::new([0; 1024 * 1024])
目前将溢出堆栈。如果您每晚使用Rust,box_syntax
功能将允许它无问题地运行:
#![feature(box_syntax)]
fn main() {
let mut buf: Box<[u8]> = box [0; 1024 * 1024]; // note box instead of Box::new()
println!("{}", buf[0]);
}
您可以在以下问题中找到有关box
和Box::new()
之间差异的其他信息:What the difference is between using the box keyword and Box::new?。