Software Development:- Function Declaration – Exceptions – Rust

Background

In the course of software application development, a good engineer will have a read of errors that can be encoutered when the application is ran.

 

Lineage

  1. Software Development:- Function Declaration – Exceptions – Java
    Link

Code

Outline

  1. Import
    • std::error::Error
  2. Module
    • Declare Constants
      • const OPERATOR_DIVIDE:char = ‘/’;
        const OPERATOR_DIVIDE_BY_ZERO:char = ‘0’;
        const OPERATOR_SUBTRACT:char = ‘-‘;
        const OPERATOR_ADDITION:char = ‘+’;
  3. Main Function
    • Declare local variables
      • let mut number1:i32;
        let mut number2:i32;
        let mut operator:char;
        let mut result:Result<i32, Box<dyn Error>>;
      • Variable Types
        • i32
          • number1
          • number2
      • Operator
        • char
          • operator
      • Result
        • i32, Box<dyn Error>
          • i32
            • number
          • Box<dyn Error>
            • Exception
    • Invoke Function Calc
      • Addition
        • Add 10 Plus 3
      • Subtract
        • Subtract 8 from 10
      • Divide
        • Divide 10  by 2
      • Divide
        • Divide by 10 by 0
        • Will raise an exception
  4. Main Function
    • Declare local variables
      • let mut number1:i32;
        let mut number2:i32;
        let mut operator:char;
        let mut result:Result<i32, Box<dyn Error>>;
      • Variable Types
        • i32
          • number1
          • number2
      • Operator
        • char
          • operator
      • Result
        • i32, Box<dyn Error>
          • i32
            • number
          • Box<dyn Error>
            • Exception
  5. Function – calc
    • Declare Function
    •  calc
          (
                operator:char
              , number1:i32
              , number2:i32
          )-> Result<i32, Box<dyn Error>>
      
    • Declare Variable
      • let result:i32;
    • Return Statement
      • return Ok(result);
        • OK
          • success
        • result
          • result of function ( int )

Code Snippet


use std::error::Error;

const OPERATOR_DIVIDE:char = '/';
const OPERATOR_DIVIDE_BY_ZERO:char = '0';
const OPERATOR_SUBTRACT:char = '-';
const OPERATOR_ADDITION:char = '+';
    
fn main() 
{
   
    
    let mut number1:i32;
    let mut number2:i32;
    let mut operator:char;
    let mut result:Result<i32, Box<dyn Error>>;
 
   
    /*
        Adding
        Add 10 + 3 = 13
    */
    number1 = 10;
    number2 = 3;
    operator = OPERATOR_ADDITION;
    result = calc(operator, number1, number2);  
  
    println!(
                "{:0?} {:1?} {:2?} is {:3?}"
                , number1
                , operator
                , number2
                , result
            );
            
   
    /*
        Subtract
        Add 10 - 8 = 2
    */            
    number1 = 10;
    number2 = 8;
    operator = OPERATOR_SUBTRACT;
    result = calc(operator, number1, number2);    
    
    println!(
                "{:0?} {:1?} {:2?} is {:3?}"
                , number1
                , operator
                , number2
                , result
            );
            
    /*
        Divide
        Add 10 / 2 = 5
    */  
    number1 = 10;
    number2 = 2;
    operator = OPERATOR_DIVIDE;
    result = calc(operator, number1, number2);
    
    
    println!(
                "{:0?} {:1?} {:2?} is {:3?}"
                , number1
                , operator
                , number2
                , result
            );     
            
    /*
        Divide
        Add 10 / 0 ???
    */              
    number1 = 10;
    number2 = 0;
    operator = OPERATOR_DIVIDE;
    result = calc(operator, number1, number2);    
    
    println!(
                "{:0?} {:1?} {:2?} is {:3?}"
                , number1
                , operator
                , number2
                , result
            );
            
    
}

fn calc
    (
          operator:char
        , number1:i32
        , number2:i32
    )-> Result<i32, Box<dyn Error>> 
{
    
    
    //declare variables
    let result:i32;
    
    //if operator is adding
    if operator == OPERATOR_ADDITION
    {
        
        result = number1 + number2;
        
    }
    //if operator is subtract
    else if operator == OPERATOR_SUBTRACT
    {
        
        result = number1 - number2;
        
    }
    //if operator is divide
    else if operator == OPERATOR_DIVIDE
    {
        
        result = number1 / number2;
        
    }
    //if operator is divide by 0
    else if operator == OPERATOR_DIVIDE_BY_ZERO
    {
        
        //result = number1 / 0;
        result =0;
        
    }
    //else set result to zero (0)
    else
    {

        result = 0;
        
    }
    
    //return OK indicator and result
    return Ok(result);
    
}

 

Output

Image

OnlineGDB

Textual


10 '+'  3 is Ok( 13)
10 '-'  8 is Ok(  2)
10 '/'  2 is Ok(  5)
thread 'main' panicked at 'attempt to divide by zero', main.rs:121:18
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


...Program finished with exit code 101
Press ENTER to exit console.

Online Code IDE

OnlineGDB

  1. rustCalcInFunction
    Link

Source Code Control

GitLab

  1. Rust – Exception Handling
    • Link
      Link
    • Files
      • 01) rustCalcInFunctionExceptionDeclared.rs

Summary

In this post, did not go too far.

Declared a function that exposes a generic error.

We did not attempt to capture the error within an error handler.

Specifically within a try/catch/finally block.

 

References

( please, will come back and add references )

 

One thought on “Software Development:- Function Declaration – Exceptions – Rust

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s