English 中文(简体)
参考Rust的um变
原标题:Returning a reference of an enum variant in Rust
  • 时间:2024-05-06 01:22:19
  •  标签:
  • rust

在我修改的以下法典中,第13章

#[derive(Debug)]
enum ShirtColor {
    Red,
    Blue,
    Yello,
}

struct Inventory {
    shirts: Vec<ShirtColor>,
}

impl< a> Inventory {
    fn giveaway(&self, user_pref: Option<& a ShirtColor>) -> & a ShirtColor {
        user_pref.unwrap_or_else(|| self.most_stocked())
    }

    fn most_stocked(&self) -> & a ShirtColor {
        let mut red_count = 0;
        let mut blue_count = 0;
        let mut yello_count = 0;

        for color in &self.shirts {
            match color {
                ShirtColor::Red => red_count += 1,
                ShirtColor::Blue => blue_count += 1,
                ShirtColor::Yello => yello_count += 1,
            }
        }

        if red_count > blue_count && red_count > yello_count {
            &ShirtColor::Red
        } else if blue_count > red_count && blue_count > yello_count {
            &ShirtColor::Blue
        } else {
            &ShirtColor::Yello
        }
    }
}

fn main() {
    let store = Inventory {
        shirts: vec![
            ShirtColor::Blue,
            ShirtColor::Red,
            ShirtColor::Blue,
            ShirtColor::Yello,
            ShirtColor::Yello,
            ShirtColor::Yello,
        ],
    };

    let user_pref1 = Some(&ShirtColor::Red);
    let giveaway1 = store.giveaway(user_pref1);
    println!(
        "The user with preference {:?} gets {:?}",
        user_pref1, giveaway1
    );

    let user_pref2 = None;
    let giveaway2 = store.giveaway(user_pref2);
    println!(
        "The user with preference {:?} gets {:?}",
        user_pref2, giveaway2
    );
}

我很想知道,为什么我可以回过来提及<代码>ShirtColor备选案文。 方法

What I think is that these values will died when we go out of the method so there will be a dangling reference here

I know I am wrong so can any one explain why this code is valid ?

问题回答

因此,为了简化这个例子,设想我们取代“<条码>多数_仓储/代码>。 采用<代码>的方法 仅返回的方法不管怎样:

fn just_red(&self) -> & a ShirtColor {
        &ShirtColor::Red
}

为什么这项工作并不等于:

fn just_red(&self) -> & a ShirtColor {
        let red = ShirtColor::Red;
        &red
}

由于疏远的参考问题,你预计不会编纂。

而基本上,这里所经历的是,王知道<代码>。 ShirtColor:Red是一个不变值,因此,如果你使用<代码>&借款,则该借款人有<代码>的静态<>代码>。 因此,<代码>&ShirtColor:Red为&un ShirtColor。 由于该功能应退还<代码>和印本;ShrtColor和的静态的有效期比<>>>>(<>静态的有效期比所有其他寿命更长),<代码>&静态ShrtColor的胁迫改为&ShrtColor待退还。 相比之下,如果我们首先对<代码>let红色 = ShirtColor:Red<>/code>具有约束力,则&red的数值为匿名寿命,在放弃当地变量<代码>><>>>>>时即告终止。 因此,<代码>和印本;red的寿命小于<>>/code>,因此&red不能被胁迫为&ShrtColor,因此不能与功能的返回类型相匹配,不能返回。





相关问题
Creating an alias for a variable

I have the following code in Rust (which will not compile but illustrates what I am after). For readability purposes, I would like to refer the same string with two different names so that the name of ...

Rust Visual Studio Code code completion not working

I m trying to learn Rust and installed the Rust extension for VSCode. But I m not seeing auto-completions for any syntax. I d like to call .trim() on String but I get no completion for it. I read that ...