< 返回版块

Snowmanzzz 发表于 2023-07-15 20:33

idea免费社区版本不能代表他们的最强实力么?

use std::convert::{TryFrom, TryInto};

#[derive(Debug, PartialEq)]
pub struct Color {
    pub red: u8,
    pub green: u8,
    pub blue: u8,
}

// We will use this error type for these `TryFrom` conversions.
#[derive(Debug, PartialEq)]
pub enum IntoColorError {
    // Incorrect length of slice
    BadLen,
    // Integer conversion error
    IntConversion,
}

// Tuple implementation
impl TryFrom<(i16, i16, i16)> for Color {
    type Error = IntoColorError;
    fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
        match tuple {
            (r, g, b) if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 => {
                Err(IntoColorError::IntConversion)
            },
            (r, g, b) => {
                Ok(Color {
                    red: r as u8,
                    green: g as u8,
                    blue: b as u8,
                })
            }
        }
    }
}

// Array implementation
impl TryFrom<[i16; 3]> for Color {
    type Error = IntoColorError;
    fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
        arr[..].try_into()
    }
}

// Slice implementation
impl TryFrom<&[i16]> for Color {
    type Error = IntoColorError;
    fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
        if slice.len() != 3 {
            return Err(IntoColorError::BadLen);
        }
        (slice[0], slice[1], slice[2]).try_into()
    }
}



arr[..].try_into()的try_into vscode能推断出这个选项

评论区

写评论
kezhuoquan 2023-07-20 21:50

idea clion 是支持ra的, 可以去设置

Hypnoes 2023-07-17 09:21

ra 毕竟有官方加持啊。

github.com/shanliu/lsys 2023-07-16 11:03

打开一个项目 半小时在初始化 醉了

ZZG 2023-07-16 09:57

所以clion不是用的ra? 一直以为是一样的...

Linyuqiz 2023-07-15 20:54

现在不是本来就是ra功能比较多嘛

1 共 5 条评论, 1 页