First rust programs
This commit is contained in:
parent
623f62025d
commit
3a5f80e153
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
**/target
|
||||||
|
**/node_modules
|
4
dining_philosophers/Cargo.lock
generated
Normal file
4
dining_philosophers/Cargo.lock
generated
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
[root]
|
||||||
|
name = "dining_philosophers"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
4
dining_philosophers/Cargo.toml
Normal file
4
dining_philosophers/Cargo.toml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
[package]
|
||||||
|
name = "dining_philosophers"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Tim Warren <twarren@nexient.com>"]
|
65
dining_philosophers/src/main.rs
Normal file
65
dining_philosophers/src/main.rs
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
use std::thread;
|
||||||
|
use std::sync::{Mutex, Arc}; // Arc == atomic reference count
|
||||||
|
|
||||||
|
struct Philosopher {
|
||||||
|
name: String,
|
||||||
|
left: usize,
|
||||||
|
right: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Philosopher {
|
||||||
|
fn new(name: &str, left: usize, right: usize) -> Philosopher {
|
||||||
|
Philosopher {
|
||||||
|
name: name.to_string(),
|
||||||
|
left: left,
|
||||||
|
right: right,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eat(&self, table: &Table) {
|
||||||
|
let _left = table.forks[self.left].lock().unwrap();
|
||||||
|
thread::sleep_ms(150);
|
||||||
|
let _right = table.forks[self.right].lock().unwrap();
|
||||||
|
|
||||||
|
println!("{} is eating.", self.name);
|
||||||
|
|
||||||
|
thread::sleep_ms(1000);
|
||||||
|
|
||||||
|
println!("{} is done eating.", self.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Table {
|
||||||
|
forks: Vec<Mutex<()>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let table = Arc::new(Table { forks: vec![
|
||||||
|
Mutex::new(()),
|
||||||
|
Mutex::new(()),
|
||||||
|
Mutex::new(()),
|
||||||
|
Mutex::new(()),
|
||||||
|
Mutex::new(()),
|
||||||
|
]});
|
||||||
|
|
||||||
|
let philosophers = vec![
|
||||||
|
Philosopher::new("Judith Butler", 0, 1),
|
||||||
|
Philosopher::new("Gilles Deleuze", 1, 2),
|
||||||
|
Philosopher::new("Karl Marx", 2, 3),
|
||||||
|
Philosopher::new("Emma Goldman", 3, 4),
|
||||||
|
Philosopher::new("Michel Foucault", 0, 4), // Left-handed to prevent deadlock
|
||||||
|
];
|
||||||
|
|
||||||
|
let handles: Vec<_> = philosophers.into_iter().map(|p| {
|
||||||
|
// This increments the refernce count
|
||||||
|
let table = table.clone();
|
||||||
|
|
||||||
|
thread::spawn(move || {
|
||||||
|
p.eat(&table);
|
||||||
|
})
|
||||||
|
}).collect();
|
||||||
|
|
||||||
|
for h in handles {
|
||||||
|
h.join().unwrap();
|
||||||
|
}
|
||||||
|
}
|
4
embed/Cargo.lock
generated
Normal file
4
embed/Cargo.lock
generated
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
[root]
|
||||||
|
name = "embed"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
8
embed/Cargo.toml
Normal file
8
embed/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "embed"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Tim Warren <twarren@nexient.com>"]
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "embed"
|
||||||
|
crate-type = ["dylib"]
|
11
embed/embed.js
Normal file
11
embed/embed.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
let ffi = require('ffi');
|
||||||
|
|
||||||
|
let lib = ffi.Library('target/release/libembed', {
|
||||||
|
process: ['void', []]
|
||||||
|
});
|
||||||
|
|
||||||
|
lib.process();
|
||||||
|
|
||||||
|
console.log("done!");
|
7
embed/embed.py
Normal file
7
embed/embed.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
from ctypes import cdll
|
||||||
|
|
||||||
|
lib = cdll.LoadLibrary("target/release/libembed.dylib")
|
||||||
|
|
||||||
|
lib.process()
|
||||||
|
|
||||||
|
print("done!")
|
11
embed/embed.rb
Normal file
11
embed/embed.rb
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
require 'ffi'
|
||||||
|
|
||||||
|
module Hello
|
||||||
|
extend FFI::Library
|
||||||
|
ffi_lib 'target/release/libembed.dylib'
|
||||||
|
attach_function :process, [], :void
|
||||||
|
end
|
||||||
|
|
||||||
|
Hello.process
|
||||||
|
|
||||||
|
puts 'done!'
|
19
embed/pure-ruby-embed.rb
Normal file
19
embed/pure-ruby-embed.rb
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
threads = []
|
||||||
|
|
||||||
|
10.times do
|
||||||
|
threads << Thread.new do
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
5_000_000.times do
|
||||||
|
count += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
count # return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
threads.each do |t|
|
||||||
|
puts "Thread finished with count=#{t.value}"
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "done!"
|
19
embed/src/lib.rs
Normal file
19
embed/src/lib.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
use std::thread;
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern fn process() {
|
||||||
|
let handles: Vec<_> = (0..10).map(|_| {
|
||||||
|
thread::spawn(|| {
|
||||||
|
let mut x = 0;
|
||||||
|
for _ in 0..5_000_000 {
|
||||||
|
x += 1
|
||||||
|
}
|
||||||
|
x // return
|
||||||
|
})
|
||||||
|
}).collect();
|
||||||
|
|
||||||
|
for h in handles {
|
||||||
|
println!("Thread finished with count={}",
|
||||||
|
h.join().map_err(|_| "Could not join a thread").unwrap());
|
||||||
|
}
|
||||||
|
}
|
41
guessing_game/Cargo.lock
generated
Normal file
41
guessing_game/Cargo.lock
generated
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
[root]
|
||||||
|
name = "guessing_game"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "advapi32-sys"
|
||||||
|
version = "0.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.3.12"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi"
|
||||||
|
version = "0.2.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-build"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
7
guessing_game/Cargo.toml
Normal file
7
guessing_game/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "guessing_game"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Tim Warren <twarren@nexient.com>"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rand = "0.3.0"
|
38
guessing_game/src/main.rs
Normal file
38
guessing_game/src/main.rs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
extern crate rand;
|
||||||
|
|
||||||
|
use std::io;
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Guess the number!");
|
||||||
|
|
||||||
|
let secret_number = rand::thread_rng().gen_range(1, 101);
|
||||||
|
|
||||||
|
// Infinite loop
|
||||||
|
loop {
|
||||||
|
println!("Please input your guess.");
|
||||||
|
|
||||||
|
let mut guess = String::new();
|
||||||
|
|
||||||
|
io::stdin().read_line(&mut guess)
|
||||||
|
.ok()
|
||||||
|
.expect("Failed to read line");
|
||||||
|
|
||||||
|
let guess: u32 = match guess.trim().parse() {
|
||||||
|
Ok(num) => num,
|
||||||
|
Err(_) => continue,
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("You guessed: {}", guess);
|
||||||
|
|
||||||
|
match guess.cmp(&secret_number) {
|
||||||
|
Ordering::Less => println!("Too small!"),
|
||||||
|
Ordering::Greater => println!("Too big!"),
|
||||||
|
Ordering::Equal => {
|
||||||
|
println!("You win!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user