some more learning programms
This commit is contained in:
parent
87b7fbdee4
commit
c3fcb60f7d
12 changed files with 262 additions and 50 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1,5 +1,5 @@
|
|||
[root]
|
||||
name = "hello_world"
|
||||
name = "Rust_Playground"
|
||||
version = "0.0.1"
|
||||
dependencies = [
|
||||
"rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
|
||||
name = "hello_world"
|
||||
name = "Rust_Playground"
|
||||
version = "0.0.1"
|
||||
authors = [ "Clara Benedicta Maria Mueller <clara.mueller@vilera.de>" ]
|
||||
|
||||
|
|
60
functions.rs
Executable file
60
functions.rs
Executable file
|
@ -0,0 +1,60 @@
|
|||
// here plays the music!
|
||||
// Syntax & Semantic
|
||||
|
||||
|
||||
// main function
|
||||
fn main() {
|
||||
let x: i32 = 5; // set x unmutable, integer 5
|
||||
|
||||
let mut y = 7; // set y mutable 5 (here an integer)
|
||||
println!("The value of y is: {}", y);
|
||||
y = 4; // change y
|
||||
println!("The value of y is: {}", y);
|
||||
|
||||
{
|
||||
println!("The value of x is: {}", x);
|
||||
let x: i32 = 7; // changing x, only inside loop
|
||||
println!("The value of x is: {}", x);
|
||||
}
|
||||
let y = "I'm a text now!"; // change y again (here as text)
|
||||
println!("The value of y is: {} ", y);
|
||||
|
||||
println!("The value of x is: {}", x);
|
||||
|
||||
print_number(88); // call function
|
||||
sum(3, 5);
|
||||
let a = add_one(x);
|
||||
|
||||
let z = (x + 2);
|
||||
println!("New values a: {}, z: {}", a, z);
|
||||
|
||||
// variable bindings with point to function
|
||||
let f: fn(i32) -> i32 = add_one; // without type inference
|
||||
//let f = add_one; // with type inference
|
||||
let six = f(5); // call function
|
||||
println!("called function says: {}", six);
|
||||
}
|
||||
|
||||
|
||||
// declaring function
|
||||
|
||||
// function for printing numbers
|
||||
fn print_number(x: i32) {
|
||||
println!("x is: {}", x);
|
||||
}
|
||||
|
||||
// function for adding two numbers
|
||||
fn sum(x: i32, y: i32) {
|
||||
println!("sum is: {}", x + y); // print sum of numbers
|
||||
}
|
||||
|
||||
// adding 1 to number
|
||||
fn add_one(i: i32) -> i32 { // -> i32 declairs return of function
|
||||
i + 1 // here no ';' -> Rust = expression based language
|
||||
// x + 1; doesn't retourn value
|
||||
}
|
||||
|
||||
// never returning function
|
||||
fn diverges() -> ! {
|
||||
panic!("This function never returns!"); // macro -> causes the current thread of execution to crash
|
||||
}
|
56
guessing_game.rs
Executable file
56
guessing_game.rs
Executable file
|
@ -0,0 +1,56 @@
|
|||
// another rust programm
|
||||
// let's get started ^.^
|
||||
|
||||
extern crate rand; // actually using rand (declared in dependencies of 'Cargo.toml')
|
||||
|
||||
// rust only uses few things by default ('prelude') -> add your needs!
|
||||
use std::io; // use io library from the standard library
|
||||
use std::cmp::Ordering; // use ordering type
|
||||
use rand::Rng; // using rands function rand::thread_rng()
|
||||
|
||||
|
||||
// main function
|
||||
fn main() {
|
||||
println!("guess a number!"); // macro, prints string
|
||||
|
||||
let secret_number = rand::thread_rng() // get copy of random number generator
|
||||
.gen_range(1, 101); // methods takes 2 arguments -> generate random number between (inclusive on the lower bound, but exclusive on the upper bound -> 1 - 100)
|
||||
|
||||
// println!("The secret number is: {}", secret_number); // print out secret number // just for testing -> runs the game!
|
||||
|
||||
|
||||
loop { // building infinity loop
|
||||
|
||||
println!("please input your guess!."); // macro, prints string
|
||||
|
||||
let mut guess = String::new(); // let -> create 'variable bindings'; String -> string type (UTF-8 encoded bit of text); '::' -> associate function; 'new()' -> created empty
|
||||
let foo = 5; // 'foo' is immutable
|
||||
let mut bar = 5; //'bar' is mutable
|
||||
|
||||
io::stdin().read_line(&mut guess) // calling associated function // without std::io -> written 'std::io::stdin()' //returns handle to standard input
|
||||
// read_line() -> calling method (like associate function, only on parcticular instant of type) an handle
|
||||
// read_line():&mut guess -> passing argument to mutable reference of guess
|
||||
.expect("failed to read line"); // io::Result encodes error handling information // not successful -> panic! with message "failed to read line"
|
||||
// alternative written: 'io::stdin().read_line(&mut guess).expect("failed to read line");'
|
||||
|
||||
// let guess: u32 = guess.trim().parse() // 'shadow' guess with new one -> convert string into u32 // 'trim()' eliminates whitespace // 'prase()' -> parses string into number
|
||||
// .expect("Please type a number!"); // stop programm if not a number
|
||||
|
||||
let guess: u32 = match guess.trim().parse() { // 'handling error' -> 'match' instead 'expect()' // result returned by prase()
|
||||
Ok(num) => num, // success -> set name num on unwrapped Ok value (integer)
|
||||
Err(_) => continue, // failure -> don't care of kind of error -> catch all _(everything not ok) // 'continue' -> move to next iteration of loop (aka ignore all errors)
|
||||
};
|
||||
|
||||
println!("you guessed: {}", guess); // print out saved string from STDIN
|
||||
println!("FOO: {} & BAR: {}", foo, bar); // print out string variables
|
||||
|
||||
match guess.cmp(&secret_number) { // cmp can called on anything comparable, takes references to thing to compare // returns odering type // match -> determine exactly what typ of ordering (ordering = enum (enumeration)) // guess + secret_number have to be same type for comparision!
|
||||
Ordering::Less => println!("To small!"), // ordering enum -> 3 possible variants (less/greater/equal)
|
||||
Ordering::Greater => println!("To big!"),
|
||||
Ordering::Equal => {
|
||||
println!("You win!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
54
looping.rs
Executable file
54
looping.rs
Executable file
|
@ -0,0 +1,54 @@
|
|||
// loops
|
||||
//
|
||||
|
||||
|
||||
// main function
|
||||
fn main() {
|
||||
|
||||
// infinity loop
|
||||
// loop {
|
||||
// println!("neverending looping!");
|
||||
// }
|
||||
|
||||
let mut x = 5; // mut x: i32
|
||||
let mut done = false; // mut done: bool
|
||||
|
||||
while !done { // looping as long as done == true
|
||||
x += x - 3; // adding 2 to x
|
||||
println!("{}", x);
|
||||
if x % 5 == 0 { // stop looping if x is multiple of 5
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
|
||||
// redone of upper while loop -> better way
|
||||
loop {
|
||||
x += x - 3; // adding 2 to x
|
||||
println!("{}", x);
|
||||
if x % 5 == 0 { // stop looping if x is multiple of 5
|
||||
break; // break out loop
|
||||
// return; // will do the same as 'break'
|
||||
}
|
||||
}
|
||||
|
||||
// for loop
|
||||
'outer: for y in 0..10 {
|
||||
'inner: for mut x in 0..10 { // from 0 to 9, 10 (upper bound is exclusive)
|
||||
x += 1 + x;
|
||||
if x % 2 == 0 { continue 'inner; } // go to next step inner loop -> only print odd numbers
|
||||
if y % 2 == 0 { continue 'outer; } // go to next step outer loop
|
||||
println!("now x is: {} and y is: {}", x, y); // x: i32
|
||||
}
|
||||
}
|
||||
|
||||
for (index, value) in (5..10).enumerate() {
|
||||
println!("index = {} value = {}", index, value);
|
||||
}
|
||||
|
||||
let line = "Hello\nWorld!".lines();
|
||||
|
||||
for (linenumber, line) in line.enumerate() {
|
||||
println!("{}: {}", linenumber, line);
|
||||
}
|
||||
}
|
||||
|
91
main.rs
91
main.rs
|
@ -1,56 +1,51 @@
|
|||
// another rust programm
|
||||
// let's get started ^.^
|
||||
|
||||
extern crate rand; // actually using rand (declared in dependencies of 'Cargo.toml')
|
||||
|
||||
// rust only uses few things by default ('prelude') -> add your needs!
|
||||
use std::io; // use io library from the standard library
|
||||
use std::cmp::Ordering; // use ordering type
|
||||
use rand::Rng; // using rands function rand::thread_rng()
|
||||
// vectors
|
||||
// jesus respawn is near!
|
||||
|
||||
|
||||
// main function
|
||||
fn main() {
|
||||
println!("guess a number!"); // macro, prints string
|
||||
// assigning vectors
|
||||
let mut v = vec![1, 2, 3, 4, 5]; // v: vec<i32>
|
||||
let w = vec![10; 0]; // vector of 10 zeroes
|
||||
|
||||
let secret_number = rand::thread_rng() // get copy of random number generator
|
||||
.gen_range(1, 101); // methods takes 2 arguments -> generate random number between (inclusive on the lower bound, but exclusive on the upper bound -> 1 - 100)
|
||||
// print particular element of v
|
||||
println!("the 3. element of v is:{}", v[2]); // counting elements beginning at 0
|
||||
|
||||
// index
|
||||
let i: usize = 0;
|
||||
// let j: i32 = 0; // doesn't work!
|
||||
println!("ite element: {}", v[i]);
|
||||
|
||||
// println!("The secret number is: {}", secret_number); // print out secret number // just for testing -> runs the game!
|
||||
|
||||
|
||||
loop { // building infinity loop
|
||||
|
||||
println!("please input your guess!."); // macro, prints string
|
||||
|
||||
let mut guess = String::new(); // let -> create 'variable bindings'; String -> string type (UTF-8 encoded bit of text); '::' -> associate function; 'new()' -> created empty
|
||||
let foo = 5; // 'foo' is immutable
|
||||
let mut bar = 5; //'bar' is mutable
|
||||
|
||||
io::stdin().read_line(&mut guess) // calling associated function // without std::io -> written 'std::io::stdin()' //returns handle to standard input
|
||||
// read_line() -> calling method (like associate function, only on parcticular instant of type) an handle
|
||||
// read_line():&mut guess -> passing argument to mutable reference of guess
|
||||
.expect("failed to read line"); // io::Result encodes error handling information // not successful -> panic! with message "failed to read line"
|
||||
// alternative written: 'io::stdin().read_line(&mut guess).expect("failed to read line");'
|
||||
|
||||
// let guess: u32 = guess.trim().parse() // 'shadow' guess with new one -> convert string into u32 // 'trim()' eliminates whitespace // 'prase()' -> parses string into number
|
||||
// .expect("Please type a number!"); // stop programm if not a number
|
||||
|
||||
let guess: u32 = match guess.trim().parse() { // 'handling error' -> 'match' instead 'expect()' // result returned by prase()
|
||||
Ok(num) => num, // success -> set name num on unwrapped Ok value (integer)
|
||||
Err(_) => continue, // failure -> don't care of kind of error -> catch all _(everything not ok) // 'continue' -> move to next iteration of loop (aka ignore all errors)
|
||||
};
|
||||
|
||||
println!("you guessed: {}", guess); // print out saved string from STDIN
|
||||
println!("FOO: {} & BAR: {}", foo, bar); // print out string variables
|
||||
|
||||
match guess.cmp(&secret_number) { // cmp can called on anything comparable, takes references to thing to compare // returns odering type // match -> determine exactly what typ of ordering (ordering = enum (enumeration)) // guess + secret_number have to be same type for comparision!
|
||||
Ordering::Less => println!("To small!"), // ordering enum -> 3 possible variants (less/greater/equal)
|
||||
Ordering::Greater => println!("To big!"),
|
||||
Ordering::Equal => {
|
||||
println!("You win!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
// handling out of bound errors
|
||||
match w.get(8) { // use 'get()' or 'get_mut()'
|
||||
Some(x) => println!("item 7 is: {}", x),
|
||||
None => println!("Sorry, this vectori is to short!")
|
||||
}
|
||||
|
||||
// iteration of vectors
|
||||
for i in &v { // using unmutable references
|
||||
println!("A reference to {}", i);
|
||||
}
|
||||
for i in &mut v {
|
||||
println!("A mutable reference to {}", i);
|
||||
}
|
||||
for i in v { // note: you cannot use vector again!
|
||||
println!("Take ownership of the vector and its elements {}", i);
|
||||
}
|
||||
|
||||
let a = 5; // a: i32
|
||||
let b = true; // b: bool
|
||||
let a2 = double(a);
|
||||
let b2 = change_truth(b);
|
||||
println!("{}", a); // works, because i32 has no pointer -> copy trait implemented
|
||||
println!("{}", b); // works -> bool has copy trait
|
||||
}
|
||||
|
||||
|
||||
// function
|
||||
fn double(x: i32) -> i32 {
|
||||
x * 2
|
||||
}
|
||||
fn change_truth(x: bool) -> bool {
|
||||
!x
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
98ae6f4d58d73a50
|
|
@ -0,0 +1 @@
|
|||
{"rustc":9749998217168482212,"target":17950575461743581120,"profile":14528549471338536373,"local":{"variant":"MtimeBased","fields":[[1492264153,198746900],[47,109,101,100,105,97,47,119,105,110,95,101,47,67,108,97,114,97,95,68,97,116,101,110,47,101,116,99,47,80,114,111,103,114,97,109,109,105,101,114,117,110,103,47,82,117,115,116,47,115,114,99,47,116,97,114,103,101,116,47,100,101,98,117,103,47,46,102,105,110,103,101,114,112,114,105,110,116,47,82,117,115,116,95,80,108,97,121,103,114,111,117,110,100,45,48,57,101,57,101,56,98,52,55,102,54,55,54,50,54,101,47,100,101,112,45,98,105,110,45,109,97,105,110,45,48,57,101,57,101,56,98,52,55,102,54,55,54,50,54,101]]},"features":"None","deps":[["rand v0.3.15",127043405904794477]],"rustflags":[]}
|
Binary file not shown.
BIN
target/debug/deps/main-09e9e8b47f67626e
Executable file
BIN
target/debug/deps/main-09e9e8b47f67626e
Executable file
Binary file not shown.
Binary file not shown.
45
tulpes_if.rs
Executable file
45
tulpes_if.rs
Executable file
|
@ -0,0 +1,45 @@
|
|||
// 'primitive' types
|
||||
// what a sunny easter day - best for programming!
|
||||
|
||||
|
||||
// main function
|
||||
fn main() {
|
||||
// assigning tulpes
|
||||
let mut x = (1, 2); // x: (i32, i32)
|
||||
let y = (2, 3); // y: (i32, i32)
|
||||
x = y; // assign tulpe into another
|
||||
|
||||
let x1 = x.0; // access first field of tulpe x
|
||||
let x2 = x.1; // access second field of tulpe x
|
||||
println!("the first element of x is: {}", x1);
|
||||
|
||||
let (a, b, c) = (1, 2, 3,); // access tulpe by destructuring
|
||||
println!("the value of a is: {}", a);
|
||||
|
||||
let d =(0,); // single element tulpe
|
||||
|
||||
|
||||
let z = 7;
|
||||
|
||||
// some looping
|
||||
if z == 5 {
|
||||
println!("z is five!");
|
||||
} else if z == 3 {
|
||||
println!("z is three!");
|
||||
}
|
||||
|
||||
else {
|
||||
println!("z is not five and not three! :(");
|
||||
}
|
||||
|
||||
// more looping, written differently
|
||||
let z = 9;
|
||||
|
||||
let y = if z == 5 {
|
||||
10
|
||||
} else {
|
||||
15
|
||||
}; // y: i32
|
||||
println!("y is defined as: {}", y);
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue