【发布时间】:2014-12-26 15:45:43
【问题描述】:
我遇到了一个错误:
extern crate rustc_serialize; // 0.3.24
use rustc_serialize::base64::{self, FromBase64, ToBase64};
fn main() {
let a: [u8; 30] = [0; 30];
let b = a.from_base64().unwrap().as_slice();
println!("{:?}", b);
}
错误:
error[E0597]: borrowed value does not live long enough
--> src/main.rs:7:13
|
7 | let b = a.from_base64().unwrap().as_slice();
| ^^^^^^^^^^^^^^^^^^^^^^^^ - temporary value dropped here while still borrowed
| |
| temporary value does not live long enough
8 | println!("{:?}", b);
9 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
不过,对我来说,代码不会出错。为什么我会出现这个错误?
【问题讨论】:
-
请注意,
let a: [u8, ..30] = [ 123, 34, ... ];可能很麻烦,因为您必须在更改数组时调整长度。如果你想记录长度,这很好,如果你不需要它并且感到痛苦,还有另一种表达方式:let a = [123u8, 34, ...];。关键是123和34只是泛型积分(没有特定类型),因此您通过指定u8来约束它们;然而,由于数组是同质容器,另一种解决方案是限制一个容器的类型(第一个,通常是123u8),编译器推断所有都是u8!
标签: rust borrow-checker