Loading s4prg_rust_01_types...
rustup doc --std, recherchez ce qui concerne les types comme i8, i16, i32, i64, i128 ainsi que les types comme u8, u16, u32, u64, u128.
rustup doc --std 'as'et convertissez des données d'un type vers un autre ; que se passe-t-il lorsque les limites ne sont pas respectées ? Étudiez
rustup doc --std 'f32'et
rustup doc --std 'f64'.
rustup doc --std 'std::convert::TryFrom'comme autre moyen d'effectuer des conversions entre ces types et montrez la différence avec as.
s4::show_mem!("u", u);.
s4::show_mem!("t[0]", t[0]);.
v.extend(0..30);par exemple).
s4::show_mem!("s[0]", &s.as_bytes()[0]);car les caractères d'une chaîne ne sont pas directement accessibles.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#[allow(unused_imports)] use crate::s4;
pub const SECTIONS: &[fn()] = &[section_1, section_2, section_3, section_4, section_5];
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fn section_1() { println!("\n~~~~ chapter 1, section 1 ~~~~"); println!("nothing more to be done, here"); }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fn section_2() { println!("\n~~~~ chapter 1, section 2 ~~~~"); let mut var = 12_u8; var += 20; println!("var: {}", var); if false { var -= 40; // PANIC!!! println!("var: {}", var); } // var = var.saturating_sub(40); println!("var: {} (saturated at lower bound)", var); var = var.wrapping_sub(40); println!("var: {} (wrapped around lower bound)", var); var = var.saturating_add(40); println!("var: {} (saturated at upper bound)", var); var = var.wrapping_add(40); println!("var: {} (wrapped around upper bound)", var); // let (res, overflow) = var.overflowing_add(230); if overflow { println!("overflow {} ~~> {}", var, res); } // let mut large = 1000_u32; let small = large as u8; println!("u32 {} ~~> u8 {}", large, small); let try_small = u8::try_from(large); println!("u32 {} ~~> try u8 {:?}", large, try_small); large /= 10; let try_small = u8::try_from(large); println!("u32 {} ~~> try u8 {:?}", large, try_small); // let floating = 987.654_f64; let integer = floating as u8; println!("f64 {} ~~> u8 {}", floating, integer); let integer = -floating as u8; println!("f64 {} ~~> u8 {}", -floating, integer); // let big_f32 = 1.2e8_f32; let small_f32 = 3.4_f32; let sum_f32 = big_f32 + small_f32; println!( "f32 {} + {} ~~> {} {}", big_f32, small_f32, sum_f32, if sum_f32 == big_f32 { "!!!" } else { "OK" } ); // let big_f64 = 1.2e8_f64; let small_f64 = 3.4_f64; let sum_f64 = big_f64 + small_f64; println!( "f64 {} + {} ~~> {} {}", big_f64, small_f64, sum_f64, if sum_f64 == big_f64 { "!!!" } else { "OK" } ); }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fn use_vector(mut v: Vec<i32>) -> Vec<i32> { v.push(999); println!("in use_vector(): {:?}", v); v }
fn use_int(i: i32) { println!("in use_int(): {:?}", i); }
fn use_string(mut s: String) -> String { s.push_str(", World!"); println!("in use_string(): {:?}", s); s }
fn section_3() { println!("\n~~~~ chapter 1, section 3 ~~~~"); let v1 = vec![10, 20, 30]; println!("v1: {:?}", v1); // let v2 = v1; // ERROR!!! moved let v2 = v1.clone(); println!("v2: {:?}", v2); println!("v1: {:?}", v1); // // use_vector(v1); // ERROR!!! moved let v3 = use_vector(v1.clone()); println!("v1: {:?}", v1); println!("v3: {:?}", v3); let i = 100; use_int(i); // let s1 = "Hello".to_owned(); println!("s1: {:?}", s1); // let s2 = s1; // ERROR!!! moved let s2 = s1.clone(); println!("s2: {:?}", s2); println!("s1: {:?}", s1); // // use_string(s1); // ERROR!!! moved let s3 = use_string(s1.clone()); println!("s1: {:?}", s1); println!("s3: {:?}", s3); }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fn section_4() { println!("\n~~~~ chapter 1, section 4 ~~~~"); let u = 98765_u32; let f = 12.34_f64; let t = [99_u16, 98, 97]; let mut v = vec![88_u16, 87, 86]; s4::show_mem!(" u", u); s4::show_mem!(" f", f); s4::show_mem!(" t", t); s4::show_mem!("t[0]", t[0]); s4::show_mem!("t[1]", t[1]); s4::show_mem!("t[2]", t[2]); s4::show_mem!(" v", v); s4::show_mem!("v[0]", v[0]); s4::show_mem!("v[1]", v[1]); s4::show_mem!("v[2]", v[2]); // v.extend(1..50); s4::show_mem!(" v", v); s4::show_mem!("v[0]", v[0]); s4::show_mem!("v[1]", v[1]); s4::show_mem!("v[2]", v[2]); // let mut s = "Short".to_owned(); s4::show_mem!(" s", s); s4::show_mem!("s[0]", s.as_bytes()[0]); s4::show_mem!("s[1]", s.as_bytes()[1]); s4::show_mem!("s[2]", s.as_bytes()[2]); // s.push_str(", and now, with more words, it becomes much longer!"); s4::show_mem!(" s", s); s4::show_mem!("s[0]", s.as_bytes()[0]); s4::show_mem!("s[1]", s.as_bytes()[1]); s4::show_mem!("s[2]", s.as_bytes()[2]); }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fn show_params( pu: u32, pf: f64, pt: [u16; 3], pv: Vec<u16>, ps: String, ) { s4::show_mem!(" pu", pu); s4::show_mem!(" pf", pf); s4::show_mem!(" pt", pt); s4::show_mem!("pt[0]", pt[0]); s4::show_mem!("pt[1]", pt[1]); s4::show_mem!("pt[2]", pt[2]); s4::show_mem!(" pv", pv); s4::show_mem!("pv[0]", pv[0]); s4::show_mem!("pv[1]", pv[1]); s4::show_mem!("pv[2]", pv[2]); s4::show_mem!(" ps", ps); s4::show_mem!("ps[0]", ps.as_bytes()[0]); s4::show_mem!("ps[1]", ps.as_bytes()[1]); s4::show_mem!("ps[2]", ps.as_bytes()[2]); }
fn section_5() { println!("\n~~~~ chapter 1, section 5 ~~~~"); let u = 98765_u32; let f = 12.34_f64; let t = [99_u16, 98, 97]; let v = vec![88_u16, 87, 86]; let s = "Short".to_owned(); s4::show_mem!(" u", u); s4::show_mem!(" f", f); s4::show_mem!(" t", t); s4::show_mem!(" t[0]", t[0]); s4::show_mem!(" t[1]", t[1]); s4::show_mem!(" t[2]", t[2]); s4::show_mem!(" v", v); s4::show_mem!(" v[0]", v[0]); s4::show_mem!(" v[1]", v[1]); s4::show_mem!(" v[2]", v[2]); s4::show_mem!(" s", s); s4::show_mem!(" s[0]", s.as_bytes()[0]); s4::show_mem!(" s[1]", s.as_bytes()[1]); s4::show_mem!(" s[2]", s.as_bytes()[2]); println!("~~~~~~~~ with .clone()"); show_params(u, f, t, v.clone(), s.clone()); println!("~~~~~~~~ without .clone()"); show_params(u, f, t, v, s); }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~