Chris Pollett > Students >
Molina

    ( Print View)

    [Bio]

    [Blog]

    [CS 297 Proposal]

    [Deliverable 1]

    [Deliverable 2]

    [Deliverable 3]

    [Deliverable 4]

    [McEliece System]

    [Regev System]

    [RSA System]

    [CS 297 Report-PDF]

    [CS 298 Proposal]

    [CS298 Slides-PDF]

    [CS298 Report-PDF]

Getting familiar with Rust

Michaela Molina (michaela.molina@sjsu.edu)

Purpose:

The purpose of this deliverable was to get familiar with Rust. To start getting familiar with Rust, I learned about Rust's strengths compared to other programming languages and what is is used for: Rust allows low-level control without compromising safety and speed. I investigated how to compile Rust, wrote a simple program in Rust, learned the advantages and basics of Rust's package manager, Cargo, and I showed my understanding of crates, Rust's library, by using two of them in a simple program.

Using Rust libraries:

The following code makes use of two crates, rand and abbreviator. Rand and abbreviator are crates from from the Rust community's crate registry, crates.io. By including the line use rand::Rng I first bring the library rand into scope because it is not brought in by default in the prelude. The prelude is a list of things that are automatically loaded into a Rust program.

In order for this line to compile, I add the line rand="0.5.5" under [dependencies] in Cargo.toml. This tells the package manager Cargo that I need an external library crate and which version I want. When Cargo sees this it will download any crates that I have not downloaded yet. When I type the line use rand::Rng I am bringing the Rng trait into scope from the rand crate. When the trait is in scope it means I can use methods that it defines. For abbreviator, I load the abbreviate method from the abbreviator crate. Correspondingly, in Cargo.toml I write abbreviator = "0.1.3" to tell Cargo that I want to load this library.

The rest of this deliverable includes slides describing what I learned about Rust. This includes how to use Cargo to create new projects, the structure of the directory it creates, how to compile with and without Cargo, and an image of my small program's output. Downloads of the material are available at the bottom of the page.

Code: Using Rust libraries

main.rs

use rand::Rng;
use abbreviator::abbreviate;

fn main() {
    println!("\n");
    println!("Use of abbreviate() from abbreviator crate:");
    println!("Abbreviation of \"hello\" is {}", abbreviate("hello"));
    println!("Abbreviation of \"san jose state university\" is {}", abbreviate("san jose state university"));
    println!("Abbreviation of \"This is a sentence.\" is {}", abbreviate("This is a sentence."));
    println!("\nUse of thread_rng() from rand crate:");
    let secret_number = rand::thread_rng().gen_range(1,101);
    println!("The random number is: {}", secret_number);
    
}

Cargo.toml

[package]
name = "my_project"
version = "0.1.0"
authors = ["Michaela Molina <michaela.molina@sjsu.edu>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.5.5"
abbreviator = "0.1.3"


Downloads: