Last active: 2 years ago
use std::slice::from_raw_parts;
use std::str::from_utf8_unchecked;
fn main() {
let (p, len) = get_memory_str();
let msg = read_memory_location(p, len);
println!("The {} bytes at {:?} stored: {}", len, p, msg)
}
fn get_memory_str() -> (*const u8, usize) {
// let str = "Hello world!";
let str = "Hello world!".to_string();
(str.as_ptr(), str.len())
}
fn read_memory_location(p: *const u8, len: usize) -> &'static str {
unsafe { from_utf8_unchecked(from_raw_parts(p, len)) }
}
use anyhow::Result;
use std::io::{stdin, BufRead};
use std::slice::from_raw_parts;
use std::str::from_utf8_unchecked;
fn main() -> Result<()> {
let (p, len) = read_user_input()?;
let msg = read_memory((p, len));
println!("The {} bytes at {:?} stored: {}", len, p, msg);
Ok(())
}
type MyPoint = (*const u8, usize);
fn read_user_input() -> Result<MyPoint> {
let mut user_input = String::new();
let stdin = stdin();
stdin.lock().read_line(&mut user_input)?;
Ok(get_memory_location(&user_input))
}
fn get_memory_location(target: &String) -> MyPoint {
(target.as_ptr(), target.len())
}
fn read_memory((p, len): MyPoint) -> &'static str {
unsafe { from_utf8_unchecked(from_raw_parts(p, len)) }
}