Skip to content

Commit dc08a7a

Browse files
authored
add initials exercises/samples
1 parent f0ec8e4 commit dc08a7a

File tree

6 files changed

+181
-0
lines changed

6 files changed

+181
-0
lines changed

exercises/transpose_exercise.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Add a transpose function using the reverse function as a template, which accepts a matrix as
2+
an argument, and returns a matrix in which two elements have been swapped.
3+
4+
fn reverse(pair: (i32, bool)) -> (bool, i32) {
5+
let (integer, boolean) = pair;
6+
7+
(boolean, integer)
8+
} */
9+
10+
fn transpose(matrix: Matrix) -> Matrix {
11+
Matrix(matrix.0, matrix.2, matrix.1, matrix.3)
12+
}
13+
14+
#[derive(Debug)]
15+
struct Matrix(f32, f32, f32, f32);
16+
17+
fn main() {
18+
let matrix = Matrix(1.1, 1.2, 2.1, 2.2);
19+
println!("{:?}", matrix);
20+
println!("---");
21+
println!("{:?}", transpose(matrix));
22+
}

first_steps/array_slices.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::mem;
2+
3+
// This function borrows a slice
4+
fn analyze_slice(slice: &[i32]) {
5+
println!("first element of the slice: {}", slice[0]);
6+
println!("the slice has {} elements", slice.len());
7+
}
8+
9+
fn main() {
10+
// Fixed-size array (type signature is superfluous)
11+
let xs: [i32; 5] = [1, 2, 3, 4, 5];
12+
13+
// All elements can be initialized to the same value
14+
let ys: [i32; 500] = [0; 500];
15+
16+
// Indexing starts at 0
17+
println!("first element of the array: {}", xs[0]);
18+
println!("second element of the array: {}", xs[1]);
19+
20+
// `len` returns the size of the array
21+
println!("array size: {}", xs.len());
22+
23+
// Arrays are stack allocated
24+
println!("array occupies {} bytes", mem::size_of_val(&xs));
25+
26+
// Arrays can be automatically borrowed as slices
27+
println!("borrow the whole array as a slice");
28+
analyze_slice(&xs);
29+
30+
// Slices can point to a section of an array
31+
println!("borrow a section of the array as a slice");
32+
analyze_slice(&ys[1 .. 4]);
33+
34+
// Out of bound indexing yields a panic
35+
// println!("{}", xs[5]);
36+
}

first_steps/formatted_print.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
fn main() {
2+
// In general, the `{}` will be automatically replaced with any
3+
// arguments. These will be stringified.
4+
println!("{} days", 31);
5+
6+
// Without a suffix, 31 becomes an i32. You can change what type 31 is,
7+
// with a suffix.
8+
9+
// There are various optional patterns this works with. Positional
10+
// arguments can be used.
11+
println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");
12+
13+
// As can named arguments.
14+
println!("{subject} {verb} {object}",
15+
object="the lazy dog",
16+
subject="the quick brown fox",
17+
verb="jumps over");
18+
19+
// Special formatting can be specified after a `:`.
20+
println!("{} of {:b} people know binary, the other half don't", 1, 2);
21+
22+
// You can right-align text with a specified width. This will output
23+
// " 1". 5 white spaces and a "1".
24+
println!("{number:>width$}", number=1, width=6);
25+
26+
// You can pad numbers with extra zeroes. This will output "000001".
27+
println!("{number:>0width$}", number=1, width=6);
28+
29+
// It will even check to make sure the correct number of arguments are
30+
// used.
31+
println!("My name is {0}, {1} {0}", "Bond", "James");
32+
// FIXME ^ Add the missing argument: "James" OK
33+
34+
// Create a structure which contains an `i32`. Name it `Structure`.
35+
#[allow(dead_code)]
36+
struct Structure(i32);
37+
38+
// However, custom types such as this structure require more complicated
39+
// handling. This will not work.
40+
// println!("This struct `{}` won't print...", Structure(3));
41+
// FIXME ^ Comment out this line OK
42+
43+
let pi = format!("{:.*}", 3, 3.141592);
44+
println!("Pi is roughly {}", pi);
45+
}

first_steps/hello.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This is the main function
2+
fn main() {
3+
// this simply print "Hello World!" to the console
4+
println!("Hello World!");
5+
println!("I'm a Rustacean!");
6+
}

first_steps/literals_operators.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
fn main() {
2+
// Integer addition
3+
println!("1 + 2 = {}", 1u32 + 2);
4+
5+
// Integer subtraction
6+
println!("1 - 2 = {}", 1i32 - 2);
7+
// TODO ^ Try changing `1i32` to `1u32` to see why the type is important OK
8+
// unsigned 1 causes the warning "attempt to subtract with overflow"
9+
10+
// Short-circuiting boolean logic
11+
println!("true AND false is {}", true && false);
12+
println!("true OR false is {}", true || false);
13+
println!("NOT true is {}", !true);
14+
15+
// Bitwise operations
16+
println!("0011 AND 0101 is {:04b}", 0b0011u32 & 0b0101);
17+
println!("0011 OR 0101 is {:04b}", 0b0011u32 | 0b0101);
18+
println!("0011 XOR 0101 is {:04b}", 0b0011u32 ^ 0b0101);
19+
println!("1 << 5 is {}", 1u32 << 5);
20+
println!("0x80 >> 2 is 0x{:x}", 0x80u32 >> 2);
21+
22+
// Use underscores to improve readability!
23+
println!("One million is written as {}", 1_000_000u32);
24+
}

first_steps/tuples.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Tuples can be used as function arguments and as return values
2+
fn reverse(pair: (i32, bool)) -> (bool, i32) {
3+
// `let` can be used to bind the members of a tuple to variables
4+
let (integer, boolean) = pair; // pattern matching
5+
6+
(boolean, integer) // return
7+
}
8+
9+
#[derive(Debug)]
10+
struct Matrix(f32, f32, f32, f32);
11+
12+
fn main() {
13+
// A tuple with a bunch of different types
14+
let long_tuple = (1u8, 2u16, 3u32, 4u64,
15+
-1i8, -2i16, -3i32, -4i64,
16+
0.1f32, 0.2f64,
17+
'a', true);
18+
19+
// Values can be extracted from the tuple using tuple indexing
20+
println!("long tuple first value: {}", long_tuple.0);
21+
println!("long tuple second value: {}", long_tuple.1);
22+
23+
// Tuples can be tuple members
24+
let tuple_of_tuples = ((1u8, 2u16, 2u32), (4u64, -1i8), -2i16);
25+
26+
// Tuples are printable
27+
println!("tuple of tuples: {:?}", tuple_of_tuples);
28+
29+
let pair = (1, true);
30+
println!("pair is {:?}", pair);
31+
32+
println!("the reversed pair is {:?}", reverse(pair));
33+
34+
// To create one element tuples, the comma is required to tell them apart
35+
// from a literal surrounded by parentheses
36+
println!("one element tuple: {:?}", (5u32,));
37+
println!("just an integer: {:?}", (5u32));
38+
39+
//tuples can be destructured to create bindings
40+
let tuple = (1, "hello", 4.5, true);
41+
42+
let (a, b, c, d) = tuple;
43+
println!("{:?}, {:?}, {:?}, {:?}", a, b, c, d);
44+
45+
let matrix = Matrix(1.1, 1.2, 2.1, 2.2);
46+
println!("{:?}", matrix)
47+
48+
}

0 commit comments

Comments
 (0)