fn align_string_with_ws(string: String, length: usize, position: Position) -> Result<String, Box<dyn Error>> {
    // Initialize a String variable to store trimmed String.
    let mut trimmed_string = String::new();
    // Trim the String.
    trimmed_string = string.trim().to_string();
    // If the trimmed String is longer than the length of the entire String that will be returned,
    // return Error.
    if trimmed_string.len() > length {
        return Err(Box::new(LengthShorterThanString));
    }
    
    // Align the String with whitespaces.
    match position {
        Position::Left => {
            let mut chars = Vec::new();
            for char in trimmed_string.chars() {
                chars.push(char);
            }
            for _ in 0..(length - trimmed_string.len()) {
                chars.push(' ');
            }
            return Ok(chars.into_iter().collect());
        },
        Position::Center => {
            let mut chars = Vec::new();
            let left_right_ws = length - trimmed_string.len();
            let mut left_ws: usize = 0;
            let mut right_ws: usize = 0;
            
            // If the number of whitespaces, needed to be inserted, is odd, 
            match left_right_ws % 2 {
                0 => {
                    left_ws = left_right_ws / 2;
                    right_ws = left_right_ws / 2;
                },
                1 => {
                    // insert one more whitespace to the right side.
                    left_ws = (left_right_ws - 1) / 2;
                    right_ws = ( (left_right_ws - 1) / 2 ) + 1;
                },
                _ => {},
            }

            for _ in 0..(left_ws) {
                chars.push(' ');
            }
            for char in trimmed_string.chars() {
                chars.push(char);
            }
            for _ in 0..(right_ws) {
                chars.push(' ');
            }
            return Ok(chars.into_iter().collect());
        },
        Position::Right => {
            let mut chars = Vec::new();
            for _ in 0..(length - trimmed_string.len()) {
                chars.push(' ');
            }
            for char in trimmed_string.chars() {
                chars.push(char);
            }
            return Ok(chars.into_iter().collect());
        },
    }
}
/// Note that the string will be trimmed before being positioned.
Note that the string will be trimmed before being positioned.
Note that the string will be trimmed before being positioned .
fn align_string_with_ws(string: String, length: usize, position: Position) -> Result<String, Box<dyn Error>> {
    // Initialize a String variable to store trimmed String.
    let mut trimmed_string = String::new();
    // Trim the String.
    trimmed_string = string.trim().to_string();
    // If the trimmed String is longer than the length of the entire String that will be returned,
    // return Error.
    if trimmed_string.len() > length {
        return Err(Box::new(LengthShorterThanString));
    }
    // Align the String with whitespaces.
    match position {
        Position::Left => {
            let mut chars = Vec::new();
            for char in trimmed_string.chars() {
                chars.push(char);
            }
            for _ in 0..(length - trimmed_string.len()) {
                chars.push(' ');
            }
            return Ok(chars.into_iter().collect());
        },
        Position::Center => {
            let mut chars = Vec::new();
            let left_right_ws = length - trimmed_string.len();
            let mut left_ws: usize = 0;
            let mut right_ws: usize = 0;
            // If the number of whitespaces, needed to be inserted, is odd, 
            match left_right_ws % 2 {
                0 => {
                    left_ws = left_right_ws / 2;
                    right_ws = left_right_ws / 2;
                },
                1 => {
                    // insert one more whitespace to the right side.
                    left_ws = (left_right_ws - 1) / 2;
                    right_ws = ( (left_right_ws - 1) / 2 ) + 1;
                },
                _ => {},
            }
            for _ in 0..(left_ws) {
                chars.push(' ');
            }
            for char in trimmed_string.chars() {
                chars.push(char);
            }
            for _ in 0..(right_ws) {
                chars.push(' ');
            }
            return Ok(chars.into_iter().collect());
        },
        Position::Right => {
            let mut chars = Vec::new();
            for _ in 0..(length - trimmed_string.len()) {
                chars.push(' ');
            }
            for char in trimmed_string.chars() {
                chars.push(char);
            }
            return Ok(chars.into_iter().collect());
        },
    }
}
fn align_string_with_ws ( string : String , length : usize , position : Position ) -> Result < String , Box < dyn Error > > { // Initialize a String variable to store trimmed String. let mut trimmed_string = String :: new ( ) ; // Trim the String. trimmed_string = string . trim ( ) . to_string ( ) ; // If the trimmed String is longer than the length of the entire String that will be returned, // return Error. if trimmed_string . len ( ) > length { return Err ( Box :: new ( LengthShorterThanString ) ) ; } // Align the String with whitespaces. match position { Position :: Left => { let mut chars = Vec :: new ( ) ; for char in trimmed_string . chars ( ) { chars . push ( char ) ; } for _ in 0 .. ( length - trimmed_string . len ( ) ) { chars . push ( ' ' ) ; } return Ok ( chars . into_iter ( ) . collect ( ) ) ; } , Position :: Center => { let mut chars = Vec :: new ( ) ; let left_right_ws = length - trimmed_string . len ( ) ; let mut left_ws : usize = 0 ; let mut right_ws : usize = 0 ; // If the number of whitespaces, needed to be inserted, is odd,  match left_right_ws % 2 { 0 => { left_ws = left_right_ws / 2 ; right_ws = left_right_ws / 2 ; } , 1 => { // insert one more whitespace to the right side. left_ws = ( left_right_ws - 1 ) / 2 ; right_ws = ( ( left_right_ws - 1 ) / 2 ) + 1 ; } , _ => { } , } for _ in 0 .. ( left_ws ) { chars . push ( ' ' ) ; } for char in trimmed_string . chars ( ) { chars . push ( char ) ; } for _ in 0 .. ( right_ws ) { chars . push ( ' ' ) ; } return Ok ( chars . into_iter ( ) . collect ( ) ) ; } , Position :: Right => { let mut chars = Vec :: new ( ) ; for _ in 0 .. ( length - trimmed_string . len ( ) ) { chars . push ( ' ' ) ; } for char in trimmed_string . chars ( ) { chars . push ( char ) ; } return Ok ( chars . into_iter ( ) . collect ( ) ) ; } , } }
Note that the string will be trimmed before being positioned.
Note that the string will be trimmed before being positioned .
string
String
length
usize
position
Position