This repository shows some examples of rust code for common coding problems. As rust is different from other programming languages, this is useful to have some patterns and code examples to translate what we know in this new paradigm.
git clone https://github.com/juli1/rust-playground.git
cd rust-playground
cargo run --bin animals
cargo run --bin main
cargo run --bin threads-unsafe
cargo run --bin threads-channel
cargo run --bin threads-rwlock
cargo test
This code illustrates the following rust concepts:
- Box: in src/trie.rs, src/listnode.rs, src/tree.rs
- Threads: in src/threads-unsafe.rs and src/threads-rwlock.rs
- Rwlock: in src/threads-rwlock.rs
- Iterator: in src/listnode.rs
- Traits: in src/animals.rs
The file src/animals.rs shows a very basic example of how to define traits when defining a new structure Animals.
The file src/listnode.rs is a quick implementation of a linkedlist with rust. The ListNode struct is used in src/main.rs
Note that this implementation also implements:
- An iterator so that you can play like
for v in node.iter() { println!("{}", n
- The Display trait to display a listnode and other next nodes so you can just
do
let n = ListNode::new(0); println!("treenode: {}", n);
The file src/tree.rs is a quick implementation of a binary tree with rust. The TreeNode struct is used in src/main.rs
The file src/trie.rs is an implementation of a trie in Rust. The TrieNode struct is used in src/main.rs to insert or search for words.
The file src/threads-unsafe.rs is a use of threads with unsafe operations (use of a global variable being modified).
The file src/threads-channel.rs is a use of threads with channels.
The file src/threads-rwlock.rs is an example of use of rwlock between multiple readers and one writer.
The file src/threads-condvar.rs shows how to use a conditional variable with Rust. It spins 6 threads and synchronize their start using a conditional variable.
Try to guess the MIME type of a file. You can see the file there: src/mime-guess.rs.