From e805f4b29892408a82b546ab1110d32ebc005402 Mon Sep 17 00:00:00 2001 From: fabian Date: Sat, 18 Mar 2023 22:02:54 +0100 Subject: [PATCH] Mitschrift Erste Lektion --- src/main.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index e7a11a9..6c06e90 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,62 @@ fn main() { - println!("Hello, world!"); +// _primitive_types(); +// _compound_types(); + _strings(); +} + + +fn _primitive_types() { + // example int + let unsigned_32_bit: u32 = 4_294_967_295; // (0..2^32-1) + let signed_32_bit: i32 = -2_147_483_648; // (-2^31..2^31-1) (java int) + println!("integers (32 bit int). unsigned: {} | signed: {}", unsigned_32_bit, signed_32_bit); + // example int (long) + let unsigned_64_bit: u64 = 18_446_744_073_709_551_615; // (0..2^64-1) + let signed_64_bit: i64 = -9_223_372_036_854_775_808; // (-2^64..2^64-1) (java long) + println!("integers (32 bit int). unsigned: {} | signed: {}", unsigned_64_bit, signed_64_bit); + // floating point numbers + let float_32_bit: f32 = -1.1; // (java float) + let float_64_bit: f64 = -1.1; // (java double) + println!("floating point nubers. 23 bit: {} | 64 bit: {}", float_32_bit, float_64_bit); +} + + +fn _compound_types() { + // arrays + let fully_initialised: [f32; 2] = [-42.1, 69.1]; + let dummy_initialised: [f32; 2] = [0.0, 0.0]; + let [head, tail]: [f32;2] = fully_initialised; + println!("arrays. prefilled: {:?} | dummy valies: {:?}", fully_initialised, dummy_initialised); + println!("array items: 0: {} 1: {}", fully_initialised[0], fully_initialised[1]); + println!("array destructured: head: {} tail: {}", head, tail); + // tuples + let tuple: (&str, i32, f64) = ("tuple", 32, 1.1); + let (string, int, float): (&str, i32, f64) = tuple; + println!("tuple: {:?}", tuple); + println!("tuple items: 0: {} 1: {} 2: {}", tuple.0, tuple.1, tuple.2); + println!("tuple destructured: string: {} int: {} float: {}", string, int, float); +} + + +fn _strings() { + // string slice + let slice: &str = "Ich bin ein Slice"; + let string: String = "Ich bin ein String".to_string(); + println!("String slice: {} | String: {}", slice, string); + // conversion + let _string2: String = String::from("Ich bin ein String2"); + let _dereferenced_string: &String = &string; + let _slice2: &str = string.as_str(); + // concatenation + let concat: String = [slice, " und ", _slice2].concat(); + println!("{}", concat); + // format + let format: String = format!("{} und {}", slice, string); + println!("{}", format); + // appending + let mut append: String = String::new(); // wichtig! mutable + append.push_str("Eine Zeile Text"); + append.push(' '); + append = append + "mit suffix"; + println!("{}", append); }