struct Person {
pub age: i32
}
pub fn main() {
let tim = Person { age: 25 };
println!("tim is {} years old", tim.age);
}
class Person {
public $age = null;
}
$tim = new Person();
$tim->age = 25;
echo sprintf(
"Tim is %d years old",
$tim->age
);
struct Person {
age: i32
}
impl Person {
pub fn new(age : i32) -> Person {
Person {
age: age
}
}
}
pub fn main() {
let tim = Person::new(25);
println!("tim is {} years old", tim.age);
}
class Person {
public $age;
private function __construct(int $age) {
$this->age = $age;
}
public static function newWithAge(int $age) : Person {
return new static($age);
}
}
$tim = Person::newWithAge(25);
echo sprintf("Tim is %d years old", $tim->age);
use std::thread;
use std::time::Duration;
fn main() {
let mut data = vec![1, 2, 3];
for i in 0..3 {
thread::spawn(move || {
data[i] += 1;
});
}
thread::sleep(Duration::from_millis(50));
}
use std::thread;
use std::time::Duration;
fn main() {
let data = vec![1, 2, 3];
for i in 0..3 {
let mut x = data[i].clone();
thread::spawn(move || {
x += 1;
});
}
thread::sleep(Duration::from_millis(50));
}
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
fn main() {
let data = Arc::new(Mutex::new(vec![1, 2, 3]));
for i in 0..3 {
let data = data.clone();
thread::spawn(move || {
let mut data = data.lock().unwrap();
data[i] += 1;
});
}
thread::sleep(Duration::from_millis(50));
}
pub fn main() {
// Embeddet den UTF-8 String "Foo" in der Binary
// erstellt eine Referenz auf den "String-Slice" und erlaubt den Zugriff via a
let a = "Foo";
}
// Frag den Ram nach Platz für ne Zval
//
// typedef union _zvalue_value {
// long lval; /* long value */
// double dval; /* double value */
// struct {
// char *val;
// int len; /* this will always be set for strings */
// } str; /* string (always has length) */
// HashTable *ht; /* an array */
// zend_object_value obj; /* stores an object store handle, and handlers */
// } zvalue_value;
//
// registriere die variable im GC
$a = "Foo";
// und prüf hin und wieder und am ende, ob die variable noch gebraucht wird
$ phpbrew install 5.4.0 +default
$ phpbrew use 5.4.22
$ curl https://sh.rustup.rs -sSf | sh
$ rustup default nightly
Package Management
cargo / composer
composer
composer.json / composer.lock
cargo
Cargo.toml / Cargo.lock
cargo
$ cargo new --bin hellophpug
$ cargo run
IDE?
PHP "Basics"
include, autoloader, namespaces, tests
Autoloader + Namespaces
mod foo;
if (
file_exists($mod.'.rs') &&
file_exists($mod.'/mod.rs')
) {
throw new \Exception(
'[file].rs or [file]/mod.rs?'
);
} else if (file_exists($mod.'.rs')) {
include $mod.'.rs';
} else if (file_exists($mod.'.rs')) {
include $mod.'/mod.rs';
}
Autoloader + Namespaces
use foo::Bar;
use foo::{Bar, Bazz};
pub use foo::Bar;
Tests
pub fn add_two(a: i32) -> i32 {
a + 2
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
assert_eq!(4, add_two(2));
}
}
Lets "write" a small webserver.
$ cargo new --bin webfoo
[dependencies]
nickel = "*"
#[macro_use] extern crate nickel;
use nickel::{Nickel, HttpRouter};
fn main() {
let mut server = Nickel::new();
server.get("/", middleware!(
let a = "Hello World";
a
));
server.get("/bar", middleware!("Bar"));
server.listen("127.0.0.1:6767");
}
$ cargo run
Ein wenig mehr OOP
struct Person {
age : i32
}
impl Person {
pub fn new(age : i32) -> Person {
Person {
age: age
}
}
pub fn get_age(&self) -> i32 {
self.age
}
}
fn main() {
let tim = Person::new(25);
println!("Tim is {} years old", tim.get_age());
}
class Person {
private $age;
function __construct($age) {
$this->age = $age;
}
function getAge() {
return $this->age;
}
}
echo sprintf(
"Tim is %d years old",
(new Person(25))->getAge()
);
fn main() {
let a;
if true {
a = "Ja";
} else {
a = "Nein";
}
println!("{}", a);
}
class Option<T> {
private T $value;
public function __construct(T $value) {
$this->value = $value;
}
public function unwrap() {
if ($this->value === null) { die('yolo'); }
return $this->value;
}
}
echo (new Option<String>("hello"))->unwrap();
echo (new Option<String>(null))->unwrap();
struct Person {
name : String
}
fn main() {
let name = "tim".to_string();
let tim = Person { name: name };
//let freerich = Person { name: name };
}
Lifetimes
struct Person<'a> {
name : &'a str
}
fn main() {
let name = "tim";
let tim = Person { name: &name };
// let freerich = Person { name: &name };
}
map/reduce
Mapper
extern crate regex;
use std::io;
use std::io::prelude::*;
use regex::Regex;
fn main() {
let stdin = io::stdin();
let regex = Regex::new(r"(\d+)$").expect("failed to compile regex");
for line in stdin.lock().lines() {
let line : String = line.expect("failed to read line");
let captures = regex
.captures(&line)
.expect("failed to capture index")
.at(0)
.expect("failed to read int");
println!("{}\t1", captures);
}
}
use std::io;
use std::io::prelude::*;
fn main() {
let stdin = io::stdin();
let mut last_number : Option<i32> = None;
let mut last_count = 0;
for line in stdin.lock().lines() {
let line : String = line.expect("failed to read line");
let mut split = line.split("\t");
let key = split
.next()
.expect("key must be given")
.parse::<i32>()
.expect("key must be an int");
let value = split
.next()
.expect("value must be given")
.parse::<i32>()
.expect("value must be an int");
if last_number.is_none() {
last_number = Some(key);
}
if last_number.unwrap() == key {
last_count += value;
continue;
}
println!(
"{}\t{}",
last_number.expect("number cant be null"),
last_count
);
last_count = 1;
last_number = Some(key);
}
if last_count != 0 && last_number.is_some() {
println!(
"{}\t{}",
last_number.expect("number cant be null"),
last_count
);
}
}