How to resolve the algorithm Conditional structures step by step in the Rust programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Conditional structures step by step in the Rust programming language

Table of Contents

Problem Statement

List the conditional structures offered by a programming language. See Wikipedia: conditionals for descriptions. Common conditional structures include if-then-else and switch. Less common are arithmetic if, ternary operator and Hash-based conditionals. Arithmetic if allows tight control over computed gotos, which optimizers have a hard time to figure out.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Conditional structures step by step in the Rust programming language

Source code in the rust programming language

// This function will only be compiled if we are compiling on Linux
#[cfg(target_os = "linux")]
fn running_linux() {
    println!("This is linux");
}
#[cfg(not(target_os = "linux"))]
fn running_linux() {
    println!("This is not linux");
}

// If we are on linux, we must be using glibc
#[cfg_attr(target_os = "linux", target_env = "gnu")]
// We must either be compiling for ARM or on a little endian machine that doesn't have 32-bit pointers pointers, on a
// UNIX like OS and only if we are doing a test build
#[cfg(all(
        any(target_arch = "arm", target_endian = "little"),
        not(target_pointer_width = "32"),
        unix,
        test
        ))]
fn highly_specific_function() {}

fn main() {
     if cfg!(target_os = "linux") {
         // Do something
     }
}

trait PrintType {
    fn print_type(&self);
}

impl PrintType for char {
    fn print_type(&self) {
        println!("char");
    }
}

impl PrintType for f64 {
    fn print_type(&self) {
        println!("64-bit float");
    }
}

fn prints_type_of_args<T, U>(arg1: &T, arg2: &U)
    where T: PrintType,
          U: PrintType
{
    arg1.print_type();
    arg2.print_type();
}

fn main() {
    prints_type_of_args(&'a', &2.0);
    prints_type_of_args(&'a', &'b');
}

if some_conditional {
    do_stuff();
} else if some_other_conditional {
    do_other_stuff();
} else {
    destroy_humanity();
}

// If statements are also expressions and will yield the value of the last expression in each block
let x = if y > z { y + 1 } else { z * 4 };

// Pattern matching may also be used
struct Point {
    x: i32,
    y: i32,
}
fn some_function(p: Option<Point>) {
    if let Some(Point { x: x_coord, y: y_coord }) = p {
        // Do something with x_coord and y_coord
    }
}

fn some_other_function(p: Option<Point>) {
    match p {
        Some(Point { x: 0, y: 0 }) => println!("Point is on origin"),
        Some(Point { x: 0, y: _ }) | Some(Point { x: _, y: 0 }) => println!("Point is on an axis"),
        Some(Point {x: a, y: b}) if a == b => println!("x and y are the same value"),
        Some(Point {x: ref mut a, y: ref b}) if *a > 4 && *b < 2 => println!("we got a mutable reference to x-value and an immutable reference to y-value."),
        op @ Some(p) => println!("op is the Option<Point> while p is the contained Point"),
        None => println!("We didn't get a point"),
    }
}

fn prints_args_dynamic(arg1: &PrintType, arg2: &PrintType) {
    arg1.print_type();
    arg2.print_type();
}
fn main() {
   prints_args_dynamic(&'a', &2.0);
   prints_args_dynamic(&6.3,&'c');
}

  

You may also check:How to resolve the algorithm Knapsack problem/Unbounded step by step in the Eiffel programming language
You may also check:How to resolve the algorithm War card game step by step in the C++ programming language
You may also check:How to resolve the algorithm Delete a file step by step in the Haskell programming language
You may also check:How to resolve the algorithm Sum of a series step by step in the Frink programming language
You may also check:How to resolve the algorithm 15 puzzle solver step by step in the C# programming language