English 中文(简体)
B. 如何按 index的关键指数获得
原标题:How to access enum by index key in rust
  • 时间:2024-02-17 02:29:04
  •  标签:
  • rust

我有恢复某种类型的<代码>的功能。 备选案文<Month>

pub fn month_from_index(index: u64) -> Option<Month>

它基本上从11-12岁恢复过来。

基本类型定义

<>Datetimeutils

/// Converts an integer in the range 1-12 into the corresponding `Month` enum.
/// Values outside the 1-12 range are converted to `None`.
pub fn month_from_index(index: u64) -> Option<Month> {
    match index {
        1 => Some(Month::January),
        2 => Some(Month::February),
        3 => Some(Month::March),
        4 => Some(Month::April),
        5 => Some(Month::May),
        6 => Some(Month::June),
        7 => Some(Month::July),
        8 => Some(Month::August),
        9 => Some(Month::September),
        10 => Some(Month::October),
        11 => Some(Month::November),
        12 => Some(Month::December),
        _ => None,
    }
}

我本月的工作时间为u64,它将履行以下职能:<代码>月_从_index。

let month = Utc::now().month() as u64;

Now... i want to get the days of the month dynamically. This days_in_month function only takes an enum, which makes it tricky.

<>main>。

use chrono::prelude::*;
use datetimeutils::{day_string, days_in_month, month_from_index, month_string, Month};
use slint::VecModel;
use std::rc::Rc;
slint::include_modules!();

fn generate_month(year: u64, month: Option<Month>) -> u64 {
    days_in_month(year, month) /// if i replace month with Month::January it works. But i want this to be be accessed via index. 
}

fn load_calendar(boxes: Rc<VecModel<NewBox>>, year: u64) {
    let current_date = chrono::Utc::now();
    let month = Utc::now().month() as u64;
    let current_month = month_from_index(month);
    let generate_days = generate_month(year, current_month);
    for new_box in 0..generate_days {
        boxes.insert(
            new_box as usize,
            NewBox {
                visible: true,
                day: new_box as i32 + 1,
            },
        )
    }
}

fn main() -> Result<(), slint::PlatformError> {
    let ui = AppWindow::new()?;
    let boxes = Rc::new(slint::VecModel::<NewBox>::from(Vec::new()));

    let new_boxes = boxes.clone();

    load_calendar(new_boxes, 2024);

    ui.set_boxes(boxes.into());

    ui.run()
}

“entergraph

 --> src/main.rs:8:25
  |
8 |     days_in_month(year, month)
  |     -------------       ^^^^^ expected `Month`, found `Option<Month>`
  |
  |
  |     arguments to this function are incorrect
  |   = note: expected enum `datetimeutils::Month`              found enum `std::option::Option<datetimeutils::Month>`
问题回答

我通过使用<代码>unwrap解决了我自己的问题。

fn generate_month(year: u64, month: Option<Month>) -> u64 {
    days_in_month(year, month.unwrap())
}

然而,不能确定为什么这样做。





相关问题
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 ...

热门标签