Skip to content
Snippets Groups Projects
Select Git revision
  • cd7a46e76b7046b8187fa24dc21db3c9870bc1ff
  • master default protected
  • v5
  • stable
4 results

foo.rs

Blame
  • bare3.rs 2.53 KiB
    //! bare3.rs
    //!
    //! String types in Rust
    //!
    //! What it covers:
    //! - Types, str, arrays ([u8;uszie]), slices (&[u8])
    //! - Iteration, copy
    //! - Semihosting (tracing)
    
    #![no_main]
    #![no_std]
    
    extern crate panic_halt;
    
    use cortex_m_rt::entry;
    use cortex_m_semihosting::{hprint, hprintln};
    
    #[entry]
    fn main() -> ! {
        hprintln!("bare3").unwrap();
        let s: &str = "ABCD";
        let bs: &[u8] = s.as_bytes();
    
        hprintln!("s = {}", s).unwrap();
        hprintln!("bs = {:?}", bs).unwrap();
    
        hprintln!("iterate over slice").unwrap();
        
        for c in bs {
            let c: &u8 = c;
            hprint!("{},", c).unwrap();
        }
    
        hprintln!("iterate iterate using (raw) indexing").unwrap();
        for i in 0..s.len() {
            let i: usize = i;
            hprintln!("{},", bs[i]).unwrap();
        }
    
        hprintln!("").unwrap();
    
        let a: [u8; 4] = [65u8; 4];
        //let mut a = [0u8; 4];
    
        hprintln!("").unwrap();
        hprintln!("a = {}", core::str::from_utf8(&a).unwrap()).unwrap();
    
        loop {}
    }
    
    // 0. Build and run the application (debug build).
    // 
    //    > cargo build --example bare3
    //    (or use the vscode build task)
    //
    // 1. What is the output in the `openocd` (Adapter Output) console?
    //
    //      bare3
    //      s = ABCD
    //      bs = [65, 66, 67, 68]
    //      iterate over slice
    //      65,66,67,68,iterate iterate using (raw) indexing
    //      65,
    //      66,
    //      67,
    //      68,
    //      
    //      
    //      a = AAAA
    //
    //    What is the type of `s`?
    //
    //    ** It is a string literal, more precise: It is a slice pointing to (&str) the string literal. **
    //
    //    What is the type of `bs`?
    //
    //    ** It is a byte slice. A dynamic array. **
    //
    //    What is the type of `c`?
    //
    //    ** It is an element of bs, a byte **
    //
    //    What is the type of `a`?
    //
    //    ** An array of type u8 with length 4. **
    //
    //    What is the type of `i`?
    //
    //    ** usize (32 or 64-bit unsigned.) from the str::len() **
    //
    //    Commit your answers (bare3_1)
    //
    // 2. Make types of `s`, `bs`, `c`, `a`, `i` explicit.
    //
    //    Commit your answers (bare3_2)
    //
    // 3. Uncomment line `let mut a = [0u8; 4];
    //`
    //    Run the program, what happens and why?
    //
    //    ** your answer here **
    //
    //    Commit your answers (bare3_3)
    //
    // 4. Alter the program so that the data from `bs` is copied byte by byte into `a`.
    //
    //    Test that it works as intended.
    //
    //    Commit your answers (bare3_4)
    //
    // 5. Look for a way to make this copy done without a loop.
    //    https://doc.rust-lang.org/std/primitive.slice.html
    //
    //    Implement and test your solution.
    //
    //    Commit your answers (bare3_5)