Rust Hack & Learn April 2019

Be Nice and have a Great Time!

We follow the Rust Code of Conduct (www.rust-lang.org/en-US/conduct.html)

In case of problem, feel free to contact either me or Alex in person or via mail/twitter

OTTO Group Digital Solutions

Big Thanks To

talent.io

Marco Neumann

@crepererum

Wrap Up

I feel I cannot control my stack usage.

Very Short Rust Intro

Hello World

fn main() {
  println!("Hello world!");
}
print("Hello world!")
#include <iostream>
 
int main () {
  std::cout << "Hello world!" << std::endl;
}
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello world!");
  }
}

Reverse String

fn reverse(s: &str) -> String {
  s.chars().rev().collect()
}
def reverse(s):
  return s[::-1]
#include <string>
#include <algorithm>
 
std::string reverse(const std::string& s) {
  std::string s2(s);
  std::reverse(s2.begin(), s2.end());
  return s2;
}
public class Reverser {
  public static String reverseString(String s) {
    return new StringBuilder(s).reverse().toString();
  }
}

Read Entire File

use std::fs::File;
use std::io::Read;
 
fn readAll(filename: &str) -> Vec<u8> {
  let mut file = File::open(filename).unwrap();
 
  let mut contents: Vec<u8> = Vec::new();
  file.read_to_end(&mut contents).unwrap();
  contents
}
def read_all(filename):
  with open(filename) as f:
    return f.read()
#include <fstream>
#include <string>
#include <iterator>
 
std::string readFile(const std::string& filename) {
  std::ifstream infile(filename);
  return std::string(
    std::istreambuf_iterator<char>(infile),
    std::istreambuf_iterator<char>(),
  );
}
import java.nio.file.*;
 
public class ReadAll {
  public static byte[] readAllBytes(String filename){
    Path file = Paths.get(filename);
    return Files.readAllBytes(file);
  }
}

Classes

pub struct MyClass {
  x: i64,
}

impl MyClass {
  pub fn new(x: i64) -> Self {
    Self { x }
  }

  pub fn f(&self, y: i64) -> i64 {
    self.x + y
  }
}
class MyClass:
  def __init__(self, x):
    self._x = x

  def f(self, y):
    return self._x + y
class MyClass {
  private:
  int x;

  public:
  MyClass(int x) {
    this->x = x;
  }

  int f(int y) {
    return x + y;
  }
};
public class MyClass {
  public MyClass(int x) {
    this.x = x
  }

  public int f(int y) {
    return x + y;
  }
}

Why Rust Is Important

use std::collections::HashSet;

fn main() {
  let mut s: HashSet<i64> = (1..=3).collect();
  for i in s.iter() {
    if *i > 2 {
      s.insert(0);
    }
  }
}
s = {1, 2, 3}
for i in s:
  if i > 2:
    s.add(0)
#include <unordered_set>

int main() {
  std::unordered_set<int> s;
  for(int i = 0; i <= 3; i++) {
      s.insert(i);
  }
  for (int i : s) {
    if (i > 0) {
      s.insert(0);
    }
  }
}
import java.util.*;

class Main {
  public static void main(String[] args) {
    HashSet<Integer> s = new HashSet<Integer>();
    for (int i=0; i <= 3; i++) { s.add(i); }

    for (int i: s) { for (int i2: s) {
      if (i2 > 2) {
        s.add(10);
      }
    }}
  }
}

Getting Started

Rust

IDE

Any editor + terminal

Docs

More at areweideyet.com

Crates

W/o Installation

Rust Playground (play.rust-lang.org)

Evcxr + Binder (goo.gl/AHsKxe)

Beginner Tasks

Rustlings (github.com/rustlings/rustlings)

Locally or via Rust Playground

Exercism (exercism.io/tracks/rust)

Nice large collections, requires special CLI client

Advanced Tasks

Call for Participation (goo.gl/66kAvr)

Different Crates

Contribute to Rust (www.rust-lang.org/en-US/contribute-community.html)

Rust Itself

Your Call / Idea

Next Meetup

at 22nd May

PhraseApp

You Talk?

Rust Hack & Learn April 2019

By Marco Neumann

Rust Hack & Learn April 2019

  • 821