Rust

Official website

The official website for Rust is https://www.rust-lang.org.

Static typing

Yes, Rust is a statically typed language. This means that variable types are known at compile time, and most type checks are performed when you build your program rather than at runtime. However, Rust also offers type inference, which lets the compiler automatically deduce the types of many variables based on their usage, reducing the need for explicit type annotations while still retaining the benefits of static type checking.

Example code

Below is an example Rust program that defines a binary search tree (BST) and includes a function to search for a value within the tree. The code defines a BST using enum-based nodes, implements insertion (to build the tree), and a search function. You can run this complete example.

#[derive(Debug)]
enum BST {
    Empty,
    Node {
        value: i32,
        left: Box<BST>,
        right: Box<BST>,
    },
}

impl BST {
    // Create an empty BST
    fn new() -> Self {
        BST::Empty
    }

    // Insert a value into the BST
    fn insert(self, val: i32) -> Self {
        match self {
            BST::Empty => BST::Node {
                value: val,
                left: Box::new(BST::Empty),
                right: Box::new(BST::Empty),
            },
            BST::Node { value, left, right } => {
                if val < value {
                    BST::Node {
                        value,
                        left: Box::new(left.insert(val)),
                        right,
                    }
                } else if val > value {
                    BST::Node {
                        value,
                        left,
                        right: Box::new(right.insert(val)),
                    }
                } else {
                    // Value already exists, do nothing.
                    BST::Node { value, left, right }
                }
            }
        }
    }

    // Search for a value in the BST
    fn find(&self, val: i32) -> bool {
        match self {
            BST::Empty => false,
            BST::Node { value, left, right } => {
                if *value == val {
                    true
                } else if val < *value {
                    left.find(val)
                } else {
                    right.find(val)
                }
            }
        }
    }
}

fn main() {
    // Create a BST and insert some values
    let bst = BST::new()
        .insert(15)
        .insert(10)
        .insert(20)
        .insert(8)
        .insert(12)
        .insert(17)
        .insert(25);

    // Search for a few values
    for key in [10, 15, 25, 99] {
        if bst.find(key) {
            println!("Found {}", key);
        } else {
            println!("Did not find {}", key);
        }
    }
}

Explanation

  1. BST Definition:
    The BST is defined as an enum that represents either an empty tree (Empty) or a node (Node) containing a value and left/right subtrees.

  2. Insertion:
    The insert method takes ownership of the tree and returns a new tree with the inserted value. It recursively inserts the value in the correct position according to BST properties.

  3. Searching:
    The find method checks whether a given value exists in the tree. It recursively navigates left or right based on comparisons.

  4. Main Function:
    A BST is created and populated with several values, and then some values are searched within the tree, printing the result to the console.

Feel free to compile and run this code with Cargo or rustc.