use std::process::{Command, Output, Stdio}; // A helper function to execute a shell command from a Rust script fn execute_command(command: &str) -> Result<(), std::io::Error> { let status = Command::new("bash") .arg("-c") .arg(command) .stderr(Stdio::inherit()) .status()?; if status.success() { Ok(()) } else { Err(std::io::Error::from_raw_os_error(status.code().unwrap_or(1))) } } let command = "echo 'your-password' | sudo -S ./chapter-1/1-null-pointer-dereference/c/null_pointer"; if let Err(err) = execute_command(command) { eprintln!("Error executing command: {}", err); } // Replace 'your-password', and you should see something like: // [sudo] password for username: bash: line 1: 64243 Done echo 'your-password' // 64943 Segmentation fault | sudo -S ./chapter-1/1-null-pointer-dereference/c/null_pointer // Error executing command: Unknown error 139 (os error 139) let ptr: *const i32 = std::ptr::null(); let value = unsafe { *ptr }; println!("Value: {}", value); // Sometimes, this code may result in a segmentation fault. let command = "echo 'your-password' | sudo -S ./chapter-1/1-null-pointer-dereference/rust/null_pointer"; if let Err(err) = execute_command(command) { eprintln!("Error executing command: {}", err); } // Replace 'your-password', and you should see something like: // [sudo] password for username: bash: line 1: 64243 Done echo 'your-password' // 64579 Segmentation fault | sudo -S ./chapter-1/1-null-pointer-dereference/rust/null_pointer // Error executing command: Unknown error 139 (os error 139) let mut vec = vec![1, 2, 3]; // let mut vec: Vec = vec![]; let item = vec.pop(); match item { Some(val) => println!("Popped value: {}", val), None => println!("Vector is empty"), } // Or let command = "rustc ./chapter-1/1-null-pointer-dereference/rust/null_pointer_safe.rs && ./chapter-1/1-null-pointer-dereference/rust/null_pointer_safe"; if let Err(err) = execute_command(command) { eprintln!("Error executing command: {}", err); } let command = "echo 'your-password' | sudo -S ./chapter-1/2-buffer-overflow/c/buffer_overflow"; if let Err(err) = execute_command(command) { eprintln!("Error executing command: {}", err); } // Replace 'your-password', and you should see: // [sudo] password for username: bash: line 1: 64243 Done echo 'your-password' // 64244 Segmentation fault | sudo -S ./chapter-1/2-buffer-overflow/c/buffer_overflow // Error executing command: Unknown error 139 (os error 139) // Replace 'your-password' let command = "echo 'your-password' | sudo -S ./chapter-1/2-buffer-overflow/rust/buffer_ovreflow"; if let Err(err) = execute_command(command) { eprintln!("Error executing command: {}", err); } let command = "echo 'your-password' | sudo -S ./chapter-1/3-garbage-collector/c++/gc"; if let Err(err) = execute_command(command) { eprintln!("Error executing command: {}", err); } struct Resource { data: Vec, } let resource = Resource { data: vec![1, 2, 3, 4, 5], }; use std::thread; let data = vec![1, 2, 3, 4, 5]; let mut handles = vec![]; for &item in &data { handles.push(thread::spawn(move || { println!("Processed: {}", item * 2); })); } for handle in handles { handle.join().unwrap(); } use std::sync::{Arc, Mutex}; use std::thread; let data = Arc::new(Mutex::new(0)); let handles: Vec<_> = (0..10) .map(|_| { let data = data.clone(); thread::spawn(move || { let mut data = data.lock().unwrap(); *data += 1; }) }) .collect(); for handle in handles { handle.join().unwrap(); } println!("Final data value: {:?}", *data.lock().unwrap()); let command = "echo 'your-password' | sudo -S ./chapter-1/4-the-era-of-multithreading-and-parallelism/c++/thread"; if let Err(err) = execute_command(command) { eprintln!("Error executing command: {}", err); } enum Shape { Circle(f64), Rectangle(f64, f64), Triangle(f64, f64, f64), } fn area(shape: Shape) -> f64 { match shape { Shape::Circle(radius) => std::f64::consts::PI * radius * radius, Shape::Rectangle(width, height) => width * height, Shape::Triangle(a, b, c) => { let s = (a + b + c) / 2.0; (s * (s - a) * (s - b) * (s - c)).sqrt() } } } area(Shape::Circle(32.0)) fn longest_common_prefix<'a>(x: &'a str, y: &'a str) -> &'a str { let min_length = std::cmp::min(x.len(), y.len()); let bytes_x = x.as_bytes(); let bytes_y = y.as_bytes(); for i in 0..min_length { if bytes_x[i] != bytes_y[i] { return &x[..i]; } } &x[..min_length] } let string1 = "abc"; let result; { let string2 = "abdef"; result = longest_common_prefix(string1, string2); } println!("The longest common prefix is: {}", result); use std::thread; let data = vec![1, 2, 3, 4, 5]; let shared_data = std::sync::Arc::new(data); let handles: Vec<_> = (0..5).map(|i| { let shared_data = shared_data.clone(); thread::spawn(move || { let local_sum: i32 = shared_data.iter().sum(); println!("Thread {} Sum: {}", i, local_sum); }) }).collect(); for handle in handles { handle.join().unwrap(); } extern "C" { fn process_data(data: *mut u8, length: usize); } fn main() { let mut data: Vec = vec![1, 2, 3, 4, 5]; unsafe { process_data(data.as_mut_ptr(), data.len()); } } // gcc -c external_lib.c -o external_lib.o // ar rcs libexternal_lib.a external_lib.o // rustc -L . -o main main.rs -l external_lib let command = "./chapter-1/8-foreign-function-interface/rust/main"; if let Err(err) = execute_command(command) { eprintln!("Error executing command: {}", err); } use std::fs::File; use std::io::{Read, Error}; fn read_file_contents(filename: &str) -> Result { let mut file = File::open(filename)?; let mut contents = String::new(); file.read_to_string(&mut contents)?; Ok(contents) } let filename = "./chapter-1/9-robust-error-handling-with-result-and-option/rust/content.txt"; match read_file_contents(filename) { Ok(contents) => { println!("File contents:\n{}", contents); } Err(err) => { eprintln!("Error reading file: {}", err); } } let mut data = [1, 2, 3, 4, 5]; let data_ptr = data.as_mut_ptr(); unsafe { *data_ptr.offset(2) = 10; } println!("Modified data: {:?}", data);