mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-29 07:19:17 +00:00
184 lines
5.1 KiB
Rust
184 lines
5.1 KiB
Rust
// try_from_into.rs
|
||
//
|
||
// TryFrom 是一種簡單且安全的類型轉換,在某些情況下可能會以可控制的方式失敗。基本上,這與 From 類似。主要的區別在於這應該返回一個 Result 類型,而不是目標類型本身。你可以在 https://doc.rust-lang.org/std/convert/trait.TryFrom.html 閱讀更多相關資訊。
|
||
//
|
||
// 執行 `rustlings hint try_from_into` 或使用 `hint` 子命令來獲取提示。
|
||
|
||
use std::convert::{TryFrom, TryInto};
|
||
|
||
#[derive(Debug, PartialEq)]
|
||
struct Color {
|
||
red: u8,
|
||
green: u8,
|
||
blue: u8,
|
||
}
|
||
|
||
// 我們將使用這個錯誤類型來進行 `TryFrom` 轉換。
|
||
#[derive(Debug, PartialEq)]
|
||
enum IntoColorError {
|
||
// 切片長度不正確
|
||
BadLen,
|
||
// 整數轉換錯誤
|
||
IntConversion,
|
||
}
|
||
|
||
// I AM NOT DONE
|
||
|
||
// 你的任務是完成此實現並返回內部類型為 Color 的 Ok 結果。你需要為三個整數的元組、三個整數的數組和整數的切片創建實現。
|
||
// 請注意,元組和數組的實現將在編譯時檢查,但切片實現需要檢查切片的長度!還要注意,正確的 RGB 顏色值必須是範圍在 0..=255 內的整數。
|
||
|
||
// 元組實現
|
||
impl TryFrom<(i16, i16, i16)> for Color {
|
||
type Error = IntoColorError;
|
||
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
|
||
}
|
||
}
|
||
|
||
// 數組實現
|
||
impl TryFrom<[i16; 3]> for Color {
|
||
type Error = IntoColorError;
|
||
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
|
||
}
|
||
}
|
||
|
||
// 切片實現
|
||
impl TryFrom<&[i16]> for Color {
|
||
type Error = IntoColorError;
|
||
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
|
||
}
|
||
}
|
||
|
||
fn main() {
|
||
// 使用 `try_from` 函數
|
||
let c1 = Color::try_from((183, 65, 14));
|
||
println!("{:?}", c1);
|
||
|
||
// 由於 Color 實現了 TryFrom,我們應該能夠使用 TryInto
|
||
let c2: Result<Color, _> = [183, 65, 14].try_into();
|
||
println!("{:?}", c2);
|
||
|
||
let v = vec![183, 65, 14];
|
||
// 使用切片我們應該使用 `try_from` 函數
|
||
let c3 = Color::try_from(&v[..]);
|
||
println!("{:?}", c3);
|
||
// 或在圓括號內使用切片並使用 TryInto
|
||
let c4: Result<Color, _> = (&v[..]).try_into();
|
||
println!("{:?}", c4);
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn test_tuple_out_of_range_positive() {
|
||
assert_eq!(
|
||
Color::try_from((256, 1000, 10000)),
|
||
Err(IntoColorError::IntConversion)
|
||
);
|
||
}
|
||
#[test]
|
||
fn test_tuple_out_of_range_negative() {
|
||
assert_eq!(
|
||
Color::try_from((-1, -10, -256)),
|
||
Err(IntoColorError::IntConversion)
|
||
);
|
||
}
|
||
#[test]
|
||
fn test_tuple_sum() {
|
||
assert_eq!(
|
||
Color::try_from((-1, 255, 255)),
|
||
Err(IntoColorError::IntConversion)
|
||
);
|
||
}
|
||
#[test]
|
||
fn test_tuple_correct() {
|
||
let c: Result<Color, _> = (183, 65, 14).try_into();
|
||
assert!(c.is_ok());
|
||
assert_eq!(
|
||
c.unwrap(),
|
||
Color {
|
||
red: 183,
|
||
green: 65,
|
||
blue: 14
|
||
}
|
||
);
|
||
}
|
||
#[test]
|
||
fn test_array_out_of_range_positive() {
|
||
let c: Result<Color, _> = [1000, 10000, 256].try_into();
|
||
assert_eq!(c, Err(IntoColorError::IntConversion));
|
||
}
|
||
#[test]
|
||
fn test_array_out_of_range_negative() {
|
||
let c: Result<Color, _> = [-10, -256, -1].try_into();
|
||
assert_eq!(c, Err(IntoColorError::IntConversion));
|
||
}
|
||
#[test]
|
||
fn test_array_sum() {
|
||
let c: Result<Color, _> = [-1, 255, 255].try_into();
|
||
assert_eq!(c, Err(IntoColorError::IntConversion));
|
||
}
|
||
#[test]
|
||
fn test_array_correct() {
|
||
let c: Result<Color, _> = [183, 65, 14].try_into();
|
||
assert!(c.is_ok());
|
||
assert_eq!(
|
||
c.unwrap(),
|
||
Color {
|
||
red: 183,
|
||
green: 65,
|
||
blue: 14
|
||
}
|
||
);
|
||
}
|
||
#[test]
|
||
fn test_slice_out_of_range_positive() {
|
||
let arr = [10000, 256, 1000];
|
||
assert_eq!(
|
||
Color::try_from(&arr[..]),
|
||
Err(IntoColorError::IntConversion)
|
||
);
|
||
}
|
||
#[test]
|
||
fn test_slice_out_of_range_negative() {
|
||
let arr = [-256, -1, -10];
|
||
assert_eq!(
|
||
Color::try_from(&arr[..]),
|
||
Err(IntoColorError::IntConversion)
|
||
);
|
||
}
|
||
#[test]
|
||
fn test_slice_sum() {
|
||
let arr = [-1, 255, 255];
|
||
assert_eq!(
|
||
Color::try_from(&arr[..]),
|
||
Err(IntoColorError::IntConversion)
|
||
);
|
||
}
|
||
#[test]
|
||
fn test_slice_correct() {
|
||
let v = vec![183, 65, 14];
|
||
let c: Result<Color, _> = Color::try_from(&v[..]);
|
||
assert!(c.is_ok());
|
||
assert_eq!(
|
||
c.unwrap(),
|
||
Color {
|
||
red: 183,
|
||
green: 65,
|
||
blue: 14
|
||
}
|
||
);
|
||
}
|
||
#[test]
|
||
fn test_slice_excess_length() {
|
||
let v = vec![0, 0, 0, 0];
|
||
assert_eq!(Color::try_from(&v[..]), Err(IntoColorError::BadLen));
|
||
}
|
||
#[test]
|
||
fn test_slice_insufficient_length() {
|
||
let v = vec![0, 0];
|
||
assert_eq!(Color::try_from(&v[..]), Err(IntoColorError::BadLen));
|
||
}
|
||
}
|