Mitschrift Erste Lektion

This commit is contained in:
fabian 2023-03-18 22:02:54 +01:00
parent 41eaa76baf
commit e805f4b298

View File

@ -1,3 +1,62 @@
fn main() { 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);
} }