但没有任何价值可借 [英] but there is no value for it to be borrowed from

查看:36
本文介绍了但没有任何价值可借的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码简化版如下所示:

fn main() {方法1(真);}fn method1(cond: bool) ->(&[u8], 字符串) {让 ret1 = 如果条件 {让 a = "base64 中的字符串".as_bytes().from_base64();a.unwrap().as_slice()} 别的 {[]//如何返回一个空数组?我应该返回向量吗?};(ret1, "aaa".to_string())}

错误:

错误:缺少生命周期说明符 [E0106]test1.rs:7 fn method1(cond: bool) ->(&[u8], 字符串) {^~~~~test1.rs:7:28: 7:33 help: 这个函数的返回类型包含一个借用的值,但是没有可以借用的值test1.rs:7 fn method1(cond: bool) ->(&[u8], 字符串) {^~~~~test1.rs:7:28: 7:33 帮助:考虑给它一个静态生命周期"test1.rs:7 fn method1(cond: bool) ->(&[u8], 字符串) {^~~~~错误:由于先前的错误而中止

最重要的是,我想从方法中返回一个数组元组和一个字符串(String 或 &str),我该怎么做?

解决方案

你不能.

你可以这样做:

fn main() {方法1(真);}fn method1(cond: bool) ->(&'static [u8], 字符串) {//' <-- 取消混淆 SO 语法突出显示...让 ret1 = 如果条件 {b"一些硬编码数据"} 别的 {乙""};(ret1, "aaa".to_string())}

<块引用>

注意:b"..." 只是用于创建 &'static [u8] 值的简写语法.

但是你不能返回一个不属于函数之外的数据的引用.如果数据不是静态已知的,那么是的,您必须返回 Vec 以传递所有权.

修订以解决评论中的问题.

其他不起作用的事情:

  • fn method1<'a>... - 首先,这并没有改变这样一个事实,即要传递引用,您必须引用仍然存在的东西函数退出后.在全局变量之外(你应该永远使用它,尤其在这种情况下),没有办法在你的函数之外调用一个长期存在的值.您必须返回一个拥有其内容的值,并且永远不能成为借用.

    声明通用生命周期参数不会改变这一点.

  • &([u8], String) - 这是无效的.首先,它有与原来完全相同的问题:你不能将借用的东西归还给你创建的东西.其次,它包含[u8],这是一个动态大小的类型.元组中不能有动态大小的类型;它们唯一有效的位置(我知道)是在结构的最末端, 会导致结构本身动态调整大小.您不能传递、返回或存储动态大小的值.

  • (&[u8], &str) - 这和原来有同样的问题,除了现在第二个元组值被借用了.在某些情况下,编译器可以为您猜测生命周期,但这不是其中之一(我不想在此处详细描述生命周期省略规则).

I have the code simplified version of which looks like the following:

fn main() {
  method1(true);
}

fn method1(cond: bool) -> (&[u8], String) {
  let ret1 = if cond {
    let a = "a string in base64".as_bytes().from_base64();
    a.unwrap().as_slice()
  } else {
    [] // how can I return an empty array? Should I return vector instead?
  };

  (ret1, "aaa".to_string())
}

The error:

error: missing lifetime specifier [E0106]
test1.rs:7 fn method1(cond: bool) -> (&[u8], String) {
                                      ^~~~~
test1.rs:7:28: 7:33 help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
test1.rs:7 fn method1(cond: bool) -> (&[u8], String) {
                                      ^~~~~
test1.rs:7:28: 7:33 help: consider giving it a 'static lifetime
test1.rs:7 fn method1(cond: bool) -> (&[u8], String) {
                                      ^~~~~
error: aborting due to previous error

The bottom line is, I want to return a tuple of an array and a string (String or &str) from a method, how can I do that?

解决方案

You can't.

You can do this:

fn main() {
  method1(true);
}

fn method1(cond: bool) -> (&'static [u8], String) {
  // ' <-- un-confuse SO syntax highlighting...
  let ret1 = if cond {
    b"some hard-coded data"
  } else {
    b""
  };

  (ret1, "aaa".to_string())
}

Note: b"..." is just a short-hand syntax for creating &'static [u8] values.

But you can't return a reference to data which isn't owned by something outside the function. If the data isn't statically known, then yes, you'll have to return a Vec<u8> in order to pass ownership out.

Revision to address question in comments.

Other things that don't work:

  • fn method1<'a>... - firstly, this doesn't change the fact that to pass a reference out, you have to be referring to something which will still exist after the function exits. Outside of globals (which you should almost never use, especially in a case like this), there's no way to summnon a long-lived value into existence outside your function. You have to return a value that owns its contents, and that can never be a borrow.

    Declaring a generic lifetime parameter doesn't change that.

  • &([u8], String) - this isn't valid. First, it has the exact same problem as originally: you can't return a borrow to something you've created. Secondly, it contains [u8], which is a dynamically sized type. You cannot have a dynamically sized type in a tuple; the only position (that I'm aware of) in which they are valid is at the very end of a struct, and that causes the struct itself to become dynamically sized. You cannot pass, return or store dynamically sized values.

  • (&[u8], &str) - this has the same problem as originally, except now the second tuple value is also borrowed. There are cases where the compiler can guess the lifetime for you, but this is not one of them (and I don't want to get into a description of the lifetime elision rules here).

这篇关于但没有任何价值可借的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆