Skip to content

A collection of examples implemented with different Rust REPLs & Runners.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

ozbe/rust-repls-and-runners

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust REPLs and Runners

A collection of examples implemented with different Rust REPLs & Runners.

Related blog post: The Three "R"s: Rust, REPL, Runner

Tools

Examples

Hello World

Code

fn main() {
  println!("Hello, world!");
}

Output

Hello, world!

Dependencies

None

File Example

Inspiration: std::fs::File - Rust

Code

use std::fs::{self, File};
use std::io::prelude::*;

fn main() -> std::io::Result<()> {
  let path = "foo.txt";

  let mut file = File::create(path)?;
  let contents = "Hello, world!";
  file.write_all(contents.as_bytes())?;
  println!("written = {}", contents);

  let mut file = File::open(path)?;
  let mut contents = String::new();
  file.read_to_string(&mut contents)?;
  println!("read = {}", contents);

  fs::remove_file(path)
}

Output

written = Hello, world!
read = Hello, world!

Dependencies

None

Rand Example

Inspiration: rand - Rust

use rand::prelude::*;

fn main() {
  let mut rng = rand::thread_rng();
  let y: f64 = rng.gen();
  println!("y = {}", y);

  let mut nums: Vec<i32> = (1..100).collect();
  nums.shuffle(&mut rng);
  println!("nums = {:?}", nums);
}

Output

y = [float between 0 and 1]
nums = [99 random numbers]

Dependencies

rand = "0.7.3"

Serde Example

Inspiration: Overview · Serde

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug)]
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let point = Point { x: 1, y: 2 };
    let serialized = serde_json::to_string(&point).unwrap();
    println!("serialized = {}", serialized);
    let deserialized: Point = serde_json::from_str(&serialized).unwrap();
    println!("deserialized = {:?}", deserialized);
}

Output

serialized = {"x":1,"y":2}
deserialized = Point { x: 1, y: 2 }

Dependencies

serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

A collection of examples implemented with different Rust REPLs & Runners.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages