Rust"错误:移出不可变的字段"

lim*_*imp 2 immutability rust

我创建了以下Rust结构:

struct HTTPRequestHeader {
    name: ~str,
    data: ~str,
    next: Option<~HTTPRequestHeader>
}
Run Code Online (Sandbox Code Playgroud)

以下代码打印出来:

fn print_headers(hdr: &HTTPRequestHeader) {
    println(fmt!("%s: %s", (*hdr).name, (*hdr).data));
    match (*hdr).next {
        Some(next_hdr) => { print_headers(next_hdr); }
        None => { }
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试编译此代码,我收到以下错误:

> rustc http_parse.rs
http_parse.rs:37:7: 37:18 error: moving out of immutable field
http_parse.rs:37    match (*hdr).next {
                          ^~~~~~~~~~~
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)

这是什么意思?包含(*hdr).name, (*hdr).data)编译而没有错误的行.该match声明不应该试图变异hdr.next以任何方式,所以我不明白怎么不变性进入它.我试图编写一个不带指针的修改版本:

fn print_headers(hdr: HTTPRequestHeader) {
    println(fmt!("%s: %s", hdr.name, hdr.data));
    match hdr.next {
        Some(next_hdr) => { print_headers(*next_hdr); }
        None => { }
    }
}
Run Code Online (Sandbox Code Playgroud)

这个给了我:

> rustc http_parse.rs
http_parse.rs:37:7: 37:15 error: moving out of immutable field
http_parse.rs:37    match hdr.next {
                          ^~~~~~~~
http_parse.rs:38:36: 38:45 error: moving out of dereference of immutable ~ pointer
http_parse.rs:38        Some(next_hdr) => { print_headers(*next_hdr); }
                                                          ^~~~~~~~~
error: aborting due to 2 previous errors
Run Code Online (Sandbox Code Playgroud)

请帮我理解这里发生了什么!:)

Luq*_*man 7

虽然该错误不告诉你,你要的值移动它没有提示你要移动它这可能有助于使其更清楚一点,为什么它不工作.

问题是您有一个唯一的指针(Option<~HTTPRequestHeader>),这意味着您需要对其进行引用或进行复制.由于它们只能拥有一个所有者,因此默认情况下会移动它们.这是Some(next_hdr)你的比赛分支中发生的事情.

所以你可能想要的是这样的:

fn print_headers(hdr: &HTTPRequestHeader) {
    println(fmt!("%s: %s", hdr.name, hdr.data));
    match hdr.next {
        Some(ref next_hdr) => print_headers(*next_hdr),
        None => {}
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,旁注:如果要通过指针访问某个结构的字段,则不需要显式取消引用它(即只是hdr.name起作用(*hdr).name).