online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
/* Import Traits */ use std::error::Error; use std::convert::TryFrom; use std::convert::TryInto; /* Declare array of seasons */ const array_of_seasons:[i32;5] = [ 1 ,2 ,3 ,4 ,5 ]; /* Declare enum Season print using debug symbols ------------------------ #[derive(Debug)] compare enum values against other enum values --------------------------------------------- #[derive(PartialEq, Eq)] */ #[derive(Debug)] #[derive(PartialEq, Eq)] enum Season { Winter = 1 , Spring = 2 , Summer = 3 , Autumn = 4 } /* Attempt to convert u16 to Enum Season */ /* How do I match enum values with an integer? https://stackoverflow.com/questions/28028854/how-do-i-match-enum-values-with-an-integer?rq=1 */ impl TryFrom<i32> for Season { type Error = (); fn try_from(v: i32) -> Result<Self, Self::Error> { match v { x if x == Season::Winter as i32 => Ok(Season::Winter), x if x == Season::Spring as i32 => Ok(Season::Spring), x if x == Season::Summer as i32 => Ok(Season::Summer), x if x == Season::Autumn as i32 => Ok(Season::Autumn), _ => Err(()), } } } /* How do I match enum values with an integer? https://stackoverflow.com/questions/58393250/returning-error-message-to-function-expecting-boxdyn-error */ fn fetchSeason ( value:i32 ) -> Result<Season, Box<dyn Error>> { //declare type Error type Error = (); //declare result as season let result:Season; if value == Season::Winter as i32 { result = Season::Winter; } else if value == Season::Spring as i32 { result = Season::Spring; } else if value == Season::Summer as i32 { result = Season::Summer; } else if value == Season::Autumn as i32 { result = Season::Autumn; } else { //raise informative error //convert string to err type return Err ( "Error Unexpected argument (value)".into() ); } //return OK and season (as result) return Ok(result); } fn getArrayElementUsingEnumValuesEqualityCheck() { /* Declare array pointer */ let mut i:u16; let mut array_element_value:i32; /* Declare function return variable */ let mut season_context:Result<Season, Box<dyn Error>>; println!("Function:- getArrayElementUsingEnumValuesEqualityCheck"); println!(""); /* Iterate array */ i =0; for array_element in array_of_seasons.iter() { //move array pointer i = i + 1; //get value that pointer is pointing to array_element_value = *array_element; //get array element as enum season_context = fetchSeason ( array_element_value ); //print array element to console println! ( "\tElement {} is {} (int) | {:?} ( enum )" , i , array_element , season_context ); } println!(""); println!(""); } fn getArrayElementsUsingTryInto() { /* Declare array pointer */ let mut i:u16; /* Declare function return variable */ let mut season_context:Result<Season, ()>; println!("Function:- getArrayElementsUsingTryInto"); println!(""); /* Iterate array */ i =0; for array_element in array_of_seasons.iter() { //move array pointer i = i + 1; //get value that pointer is pointing to let array_element_value:i32 = *array_element; //try to convert int into enum value //using tryinto season_context = <i32 as TryInto<Season>>::try_into ( array_element_value ); //print array element to console println! ( "\tElement {} is {} (int) | {:?} ( enum )" , i , array_element , season_context ); } println!(""); println!(""); } fn getArrayElementsUsingTryFrom() { /* Declare array pointer */ let mut i:u16; /* declare value of array element */ let mut array_element_value:i32; /* Declare function return variable */ let mut season_context:Result<Season, ()>; println!("Function:- getArrayElementsUsingTryFrom"); println!(""); /* Iterate array */ i =0; for array_element in array_of_seasons.iter() { //move array pointer i = i + 1; //get value that pointer is pointing to array_element_value = *array_element; //attempt to get season enum from int //use try_from //implemented earlier season_context = Season::try_from ( array_element_value ); //print array element to console println! ( "\tElement {} is {} (int) | {:?} ( enum )" , i , array_element , season_context ); } println!(""); println!(""); } fn main() { getArrayElementUsingEnumValuesEqualityCheck(); getArrayElementsUsingTryInto(); getArrayElementsUsingTryFrom(); }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue