Rust 比较切片和寿命

Rust 比较切片和寿命,rust,Rust,我正在尝试将rust fuse代码(我认为是在0.12上编译的)更新到最新的夜间版本。改变的事情之一是从[T]到&[T]的隐式强制被删除。我更改了函数调用,但以下代码()有问题: 这会引发以下错误: src/reply.rs:580:33: 580:57 error: borrowed value does not live long enough src/reply.rs:580 assert!(bytes == &[&[0x12, 0x34, 0x56

我正在尝试将rust fuse代码(我认为是在0.12上编译的)更新到最新的夜间版本。改变的事情之一是从
[T]
&[T]
的隐式强制被删除。我更改了函数调用,但以下代码()有问题:

这会引发以下错误:

src/reply.rs:580:33: 580:57 error: borrowed value does not live long enough
src/reply.rs:580             assert!(bytes == &[&[0x12, 0x34, 0x56, 0x78]]);
                                                 ^~~~~~~~~~~~~~~~~~~~~~~~
<std macros>:1:1: 12:2 note: in expansion of assert!
src/reply.rs:580:13: 580:60 note: expansion site
src/reply.rs:579:33: 581:10 note: reference must be valid for an anonymous lifetime defined on the block at 579:32...
src/reply.rs:579         as_bytes(&data, |bytes| {
src/reply.rs:580             assert!(bytes == &[&[0x12, 0x34, 0x56, 0x78]]);
src/reply.rs:581         });
<std macros>:3:12: 580:58 note: ...but borrowed value is only valid for the expression at 3:11
<std macros>:3         if !$cond {
<std macros>:4             panic!(concat!("assertion failed: ", stringify!($cond)))
<std macros>:5         }
<std macros>:6     );
<std macros>:7     ($cond:expr, $($arg:expr),+) => (
<std macros>:8         if !$cond {
               ...
<std macros>:1:1: 12:2 note: in expansion of assert!
src/reply.rs:580:13: 580:60 note: expansion site
src/reply.rs:580:33:580:57错误:借来的值寿命不够长
src/reply.rs:580断言!(字节==&[&[0x12、0x34、0x56、0x78]];
^~~~~~~~~~~~~~~~~~~~~~~~
:1:1:12:2注意:在断言的扩展中!
src/reply.rs:580:13:580:60注:扩建场地
src/reply.rs:579:33:581:10注意:引用必须在579:32块上定义的匿名生存期内有效。。。
src/reply.rs:579 as_字节(&data,|字节|{
src/reply.rs:580断言!(字节==&[&[0x12,0x34,0x56,0x78]];
src/reply.rs:581});
:3:12:580:58注:……但借用值仅对3:11处的表达式有效
:3如果$康德{
:4恐慌!(concat!(“断言失败:”,stringify!($cond)))
:5         }
:6     );
:7($cond:expr,$($arg:expr),+)=>(
:8如果!$cond{
...
:1:1:12:2注意:在断言的扩展中!
src/reply.rs:580:13:580:60注:扩建场地
问题:

  • 这是比较切片内容的正确方法吗
  • 为什么会触发错误

  • 对于后人来说,最新的夜间写作版本是rustc 0.13.0-nightly(81eeec094 2014-11-21 23:16:48+0000)用户夏普在IRC上帮了我的忙

    我将代码片段更改为

    let data: [u8, ..4] = [0x12, 0x34, 0x56, 0x78];
    as_bytes(&data, |bytes| {
        assert_eq!(bytes, [[0x12, 0x34, 0x56, 0x78].as_slice()].as_slice());
    });
    
    它编译并确实比较了片段的内容。至于到底为什么会触发错误,我还不太清楚

    let data: [u8, ..4] = [0x12, 0x34, 0x56, 0x78];
    as_bytes(&data, |bytes| {
        assert_eq!(bytes, [[0x12, 0x34, 0x56, 0x78].as_slice()].as_slice());
    });