- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了一个错误:
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
不过,对我而言,代码不会出错。为什么会出现该错误?
最佳答案
这里的问题是您没有将 from_base64
的结果存储在任何地方,然后通过调用 as_slice
引用它。像这样的链接调用会导致 from_base64
的结果在行尾超出范围,并且引用不再有效。
extern crate rustc_serialize; // 0.3.24
use rustc_serialize::base64::FromBase64;
fn main() {
let a: [u8; 30] = [0; 30];
let b = a.from_base64().unwrap();
println!("{:?}", b.as_slice());
}
关于rust - 使用 as_slice() 时为 "borrowed value does not live long enough",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41844072/
我不明白为什么这段代码可以编译: fn f(v: &mut Vec) -> &[isize] { v.as_mut_slice() } 这不是: fn f(v: &mut Vec) -> &[
最低代码: fn foo() { let vector = vec![1u8, 2u8]; let a = &vector.as_slice()[0]; drop(vector
针对较新的 nightlies 重新编译一些较旧的代码,我收到有关使用 as_slice() 语法以支持 var[] 语法的警告。 然而,当我将 as_slice() 替换为 [] 时,如图 in t
我有以下功能: fn i_to_str(&self, i: int) -> &'a str { return i.to_string().as_slice(); } 由
我遇到了一个错误: extern crate rustc_serialize; // 0.3.24 use rustc_serialize::base64::{self, FromBase64, To
这个问题在这里已经有了答案: "borrowed value does not live long enough" when using as_slice() (1 个回答) 关闭 5 年前。 我一
我遇到了一个错误: extern crate rustc_serialize; // 0.3.24 use rustc_serialize::base64::{self, FromBase64, To
我是一名优秀的程序员,十分优秀!