匹配时如何借用期权?

时间:2018-06-08 03:09:03

标签: rust

我有以下代码:

<section id="passions-section">
    <div class="container xs-col-12 col-6">
        <h1 class="display-5 text-center">Passions</h1>
        <div class="row">
            <div class="xs-col-12 col-6 passions square-1"><img class="img-fluid rounded" src="./img/guitar.jpg"></div>
            <div class="xs-col-12 md-col-6 passions square-1-text text-center">
                <div class="vertical-align">
                    <p>Music and art are a core part of my personality</p>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="xs-col-12 md-col-6 passions square-2-text text-center desktop">
                <div class="vertical-align">
                    <p>Music and art are a core part of my personality</p>
                </div>
            </div>
            <div class="xs-col-12 md-col-6 passions square-2"><img class="img-fluid rounded" src="./img/camera.jpg"></div>
            <div class="xs-col-12 md-col-6 passions square-2-text text-center mobile">
                <div class="vertical-align">
                    <p>Music and art are a core part of my personality</p>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="xs-col-12 md-col-6 passions square-3"><img class="img-fluid rounded" src="./img/paint.jpg"></div>
            <div class="xs-col-12 md-col-6 passions square-3-text text-center">
                <div class="vertical-align">
                    <p>Music and art are a core part of my personality</p>
                </div>
            </div>
        </div>
    </div>
</section>  
    .mobile {
        display: none;
    }
    @media only screen and (max-width: 640px) {
        .mobile {
            display: block;
        }
        .desktop {
            display: none;
        }
    }
    #passions-section {
        padding: 2rem 0 2rem 0;
    }
    #passions-section h1 {
        padding-bottom: 1rem;
    }
    #passions-section .container {
        margin-bottom: 2rem;
    }
    .passions {
        background-color: #ffffff;
        box-shadow: 0px 0px 40px -10px rgba(59, 66, 71, 1);
    }
.vertical-align{
    display: inline-block;
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}
.passions p{
    font-size: 1.2rem;
    padding: 0 1rem 0 1rem;
}
.passions img{
    height: auto;
    width: 100%;
    opacity: 0.8;
}

这对我来说很糟糕。而不是检查fn remove_descendent(&mut self, key: &K) -> Option<V> { if self.left.is_some() && self.left.as_ref().unwrap().key == *key { return self.remove_left(); } // more of the function } 然后解开。我认为我真正应该做的是使用匹配语句解构is_some变量所代表的Option,如下所示:

left

然而,当我这样做时,我收到以下错误:

fn remove_descendent(&mut self, key: &K) -> Option<V> {
    match self.left {
        Some(ref left) if left.key == *key => self.remove_left(),
        _ => None
    }

我想我得到了我不能不可挽回地借用一个结构成员然后可变地借用结构。但如果是这样的话,那么模仿匹配error[E0502]: cannot borrow `*self` as mutable because `self.left.0` is also borrowed as immutable --> src/lib.rs:29:51 | 29 | Some(ref left) if left.key == *key => self.remove_left(), | -------- ^^^^ mutable borrow occurs here | | | immutable borrow occurs here 30 | _ => None 31 | } | - immutable borrow ends here 的正确方法是什么?有吗?

1 个答案:

答案 0 :(得分:3)

编译器抱怨,因为在匹配臂self仍然借用。你可以事先克隆key来解决这个问题:

fn remove_descendent(&mut self, key: &K) -> Option<V> {
    match self.left.clone() {
        Some(ref left) if left.key == *key => self.remove_left(),
        _ => None,
    }
}

您可以在行动on the Playground中看到这一点。

启用Non Lexical Lifetimes后,您的代码就可以正常编译:Playground