Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanoz committed Apr 5, 2024
0 parents commit 0d4901a
Show file tree
Hide file tree
Showing 45 changed files with 2,692 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/pages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Pages

on:
push:
branches:
- main

concurrency:
group: ${{ github.ref }}-pages
cancel-in-progress: true

jobs:
publish-pages:
runs-on: ubuntu-latest
container: node:21
permissions:
contents: write
steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: npm install -g backslide
- name: Export presentation
run: bs export && mv ./dist/presentation.html ./dist/index.html
working-directory: ./presentation
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./presentation/dist
18 changes: 18 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: CI

on: [push]

concurrency:
group: ${{ github.ref }}-test
cancel-in-progress: true

jobs:
test:
runs-on: ubuntu-latest
container: rust:1.76
steps:
- uses: actions/checkout@v3
- run: cargo build
working-directory: ./exercises
- run: cargo test
working-directory: ./exercises
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
presentation/.tmp
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Daan Sieben

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Rust hands-on, presentation with exercises

Link to [presentation](https://daanoz.github.io/rust-hands-on/).

This hands-on is targeted at rust beginners.

## Prerequisites

The workshop practices require that Rust is installed on your system.

Official install instructions: [https://www.rust-lang.org/tools/install](https://www.rust-lang.org/tools/install)

```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```

Include `~/.cargo/bin` in your PATH environment variable, so rustc (the compiler),
cargo (build/dependency tool) and rustup can be found.

Visual Studio Code is a convenient IDE for working with Rust. It is recommended to
install the rust-analyzer extension, so during coding you get code completion,
errors shown without having to compile first, ... The CodeLLDB debugger extension
allows for debugging of your source code.

## Running the presentation

First install backslide:

```bash
npm install -g backslide
```

To run the presentation, change into the presentation folder, and run:

```bash
$ bs s
```
3 changes: 3 additions & 0 deletions exercises/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "rust-hands-on-exercises"
edition = "2021"
44 changes: 44 additions & 0 deletions exercises/src/bin/exercise_00.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/// # Supported primitive types
///
/// Nothing needs to be done for this exercise, it is just to show the primitive types.
///

#[allow(unused_variables)]
fn exercise() {
// Primitive types
let bool_value = true; // OR let bool_value: bool = true;

let u8_value: u8 = 20; // OR let u8_value = 20u8;
let u16_value: u16 = 20; // OR let u16_value = 20u16;
let u32_value: u32 = 20; // OR let u32_value = 20u32;
let u64_value: u64 = 20; // OR let u64_value = 20u64;
let u128_value: u128 = 20; // OR let u128_value = 20u128;
let usize_value: usize = 20; // OR let usize_value = 20usize; takes 4 bytes on 32-bit systems and 8 bytes on 64-bit systems

let i8_value: i8 = 20; // OR let i8_value = 20i8;
let i16_value: i16 = 20; // OR let i16_value = 20i16;
let i32_value = 20; // OR let i32_value: i32 = 20; OR let i32_value = 20i32; Default type for integers
let i64_value: i64 = 20; // OR let i64_value = 20i64;
let i128_value: i128 = 20; // OR let i128_value = 20i128;
let isize_value: isize = 20; // OR let isize_value = 20isize; takes 4 bytes on 32-bit systems and 8 bytes on 64-bit systems

let f32_value: f32 = 20.0; // OR let f32_value = 20f32;
let f64_value = 20.0; // OR let f64_value: f64 = 20; OR let f64_value = 20f64; Default type for floats

let str_value = "hello world"; // Reference to a string slice
let char_value = 'a'; // OR let char_value: char = 'a';

let array_type: [i32; 5] = [1, 2, 3, 4, 5]; // OR let array_type = [1, 2, 3, 4, 5]; Note that only if the elements are primitive types, the array itself can be treated as primitive.
}

fn main() {
exercise();
}

#[cfg(test)]
mod tests {
#[test]
fn test_main() {
assert!(true);
}
}
28 changes: 28 additions & 0 deletions exercises/src/bin/exercise_01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// # Variables
///
/// In this case we are trying to mutate a variable that is declared as immutable.
///

fn exercise() -> i32 {
let output = 7;

// Do not change below this line __________________________________________
println!("The value of output is: {}", output);
output = 5;
println!("The value of output is: {}", output);
output
}

fn main() {
exercise();
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_main() {
assert_eq!(exercise(), 5);
}
}
27 changes: 27 additions & 0 deletions exercises/src/bin/exercise_02.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// # Variables
///
/// Alternative solution to the problem in exercise_01.rs
///

fn exercise() -> i32 {
let output = 7; // Do not change this line

println!("The value of output is: {}", output);
output = 5;
println!("The value of output is: {}", output);
output // Do not change this line
}

fn main() {
exercise();
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_main() {
assert_eq!(exercise(), 5);
}
}
29 changes: 29 additions & 0 deletions exercises/src/bin/exercise_03.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// # Variables
///
/// The type of a variable cannot be changed, even not if mutable. How can we fix the code below
/// when we still want to assign the integer to variable `output`?
///

#[allow(unused_mut)] // extra: after solving the problem, remove this line and observe the warning
fn exercise() -> i32 {
let mut output = "seven"; // Do not change this line

println!("The value of output is: {}", output);
output = 5;
println!("The value of output is: {}", output);
output // Do not change this line
}

fn main() {
exercise();
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_main() {
assert_eq!(exercise(), 5);
}
}
30 changes: 30 additions & 0 deletions exercises/src/bin/exercise_04.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// # Scopes
///
/// Using the same variable name in different scopes is allowed in Rust. How to tell the compiler
/// that the variable in the inner scope is different from the variable in the outer scope?
///

#[allow(unused_mut, unused_variables)]
fn exercise() -> i32 {
let mut output = 7; // Do not change this line
{
println!("The value of output is: {}", output);
output = 5; // <-- how to tell the compiler that this is a different variable?
println!("The value of output is: {}", output);
}
output // Do not change this line
}

fn main() {
exercise();
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_main() {
assert_eq!(exercise(), 7);
}
}
28 changes: 28 additions & 0 deletions exercises/src/bin/exercise_05.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// # Functions
///
/// There is an issue in this code snippet! Can you find it?
///

fn exercise() -> i32 {
// Do not change this function
let value = 7;
square(value)
}

fn square(value: i32) -> i32 {
value * value;
}

fn main() {
exercise();
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_main() {
assert_eq!(exercise(), 49);
}
}
37 changes: 37 additions & 0 deletions exercises/src/bin/exercise_06.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// # Functions
///
/// There are two issues in this code snippet! Can you find them?
///

fn exercise() -> i32 {
// Do not change this function
let value = 7;
divide(value)
}

fn divide(value: i32) {
if value == 0 {
0
} else {
value / value
};
}

fn main() {
exercise();
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_main() {
assert_eq!(exercise(), 1);
}

#[test]
fn test_divide_0() {
assert_eq!(divide(0), 0);
}
}
33 changes: 33 additions & 0 deletions exercises/src/bin/exercise_07.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// # Ownership
///
/// Given the following code, which of the lines 13-16 are invalid? And why?
/// And why are the others valid?
///

#[allow(unused)]
fn exercise() -> String {
// Do not change this function
let a = 5;
let b = a;
let c = String::from("five");
let d = c;

// format!("{a}={c}")
// format!("{b}={c}")
// format!("{a}={d}")
// format!("{b}={d}")
}

fn main() {
exercise();
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_main() {
assert_eq!(exercise(), String::from("5=five"));
}
}
Loading

0 comments on commit 0d4901a

Please sign in to comment.