rust programming language

Learn from examples

1. Program to declare and print integer, double, and string exru1.ru source code // exru1.ru declare and print integer, double, string fn main() { let i = 7; // immutable can not be changed let x = 3.5; let name = "some name"; let yes = true; let no = false; let mut y = 4.0; // mut needed if going to change y = y + 0.5; // Print text to the console println!("exru1.ru running"); println!("i= {:?} ",i); println!("x= {:?}",x); println!("name= {:?}",name); println!("yes= {:?}",yes); println!("no= {:?}",no); println!("y= {:?}",y); println!("types are i32 i64 f32 f64 boolean ... "); println!(" "); println!("exru1.ru finished"); } Output from execution: exru1.ru running i= 7 x= 3.5 name= "some name" yes= true no= false y= 4.5 types are i32 i64 f32 f64 boolean ... exru1.ru finished get_type.ru source code // get_type.ru use std::any::type_name; fn type_of(_: T) -> &'static str { type_name::() } fn main() { println!("get_type.ru running"); println!(" "); let j = 21; let i:i64 = 10; let k:i32 = 9; let y = 2.5; let x:f32 = 6.0; let name = "mine"; let yes = true; let n = 10; let mut _a = vec![0.0_f64; n]; let mut _b = vec![vec![0.0_f64; n]; n]; println!("j {}", type_of(j)); println!("i {}", type_of(i)); println!("k {}", type_of(k)); println!("y {}", type_of(y)); println!("x {}", type_of(x)); println!("name {}", type_of(name)); println!("yes {}", type_of(yes)); println!("n {}", type_of(n)); println!("_a {}", type_of(_a)); println!("_b {}", type_of(_b)); println!(" "); println!("get_type.ru finished"); } Execution output get_type.ru running j i32 i i64 k i32 y f64 x f32 name &str yes bool n usize _a alloc::vec::Vec _b alloc::vec::Vec> get_type.ru finished 2. commands to execute the source code at a minimum, Windows, Linux, MacOSX. Windows: optional write output to file rustc exru1.ru exru1 exru1 > exru1_ru.out Linux: rustc exru1.ru ./exru1 ./exru1 > exru1_ru.out MacOSX rustc exru1.ru exru1 3. You must be able to declare variables and arrays and matrix of various types. exru3.ru source code // exru3.ru declare and use vector and matrix fn main() { println!("exru3.ru running"); let mut xs = vec![0, 1, 2]; println!("Initial xs= {:?}", xs); // Insert new element at the end of the vector println!("Push 3 onto xs"); xs.push(3); println!("longer xs= {:?}", xs); println!("xs length= {}", xs.len()); println!("xs[2]= {}", xs[2]); println!("Pop last element of xs= {:?}", xs.pop()); println!("xs length= {}", xs.len()); let n = 3; let mut _xt = vec![0.0_f64; n]; println!("Initial xt= {:?}", _xt); println!("xt length= {}", _xt.len()); _xt[1] = 1.1; _xt[2] = 2.2; println!("updated xt= {:?}", _xt); let mut _a = vec![vec![0.0_f64; n]; n]; println!("initial a= {:?}", _a); _a[0][0] = 1.0; _a[1][1] = 1.0; _a[2][2] = 1.0; println!("updated a= {:?}", _a); let mut _b = vec![vec![0.0_f64; n]; n]; _b[0][0] = 2.0; _b[1][1] = 2.0; _b[2][2] = 2.0; println!("updated b= {:?}", _b); let mut _c = vec![vec![0.0_f64; n]; n]; for j in 0..n { // matrix multiply for i in 0..n { for k in 0..n { _c[i][j] += _a[i][k] * _b[k][j]; } } } println!("c=a*b= {:?}", _c); println!("exru3.ru finished"); } Execution output: exru3.ru running Initial xs= [0, 1, 2] Push 3 onto xs longer xs= [0, 1, 2, 3] xs length= 4 xs[2]= 2 Pop last element of xs= Some(3) xs length= 3 Initial xt= [0.0, 0.0, 0.0] xt length= 3 updated xt= [0.0, 1.1, 2.2] initial a= [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] updated a= [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]] updated b= [[2.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 2.0]] c=a*b= [[2.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 2.0]] exru3.ru finished 4. You need to be able to have loops, iteration statements exru4.ru source code // exru4.ru loops iteration for while fn main() { println!("exru4.ru running"); for i in 0..5 { println!("i= {:?} ",i); } println!(" "); for j in 1..10 { if j==3 { continue; // skip 3 } else if j>7 { break; // no 8,9,10 } println!("j= {:?} ",j); } println!(" "); let mut x = 4.0; while x >= 1.0 { println!("x= {:?}",x); x = x - 1.0; } println!(" "); println!("exru4.ru finished"); } Execution output: exru4.ru running i= 0 i= 1 i= 2 i= 3 i= 4 j= 1 j= 2 j= 4 j= 5 j= 6 j= 7 x= 4.0 x= 3.0 x= 2.0 x= 1.0 exru4.ru finished 5. You need if else conditional statements exru5.ru source code // exru5.ru if else fn main() { println!("exru5.ru running"); let mut j = 3; if j > 2 && j < 5 { println!("j= {:?} ",j); } let mut x = 1.0; if x <= 2.0 || x >= 4.0 { println!("x= {:?} ",x); } j = 5; x = 4.5; if j == 3 { println!("j= {:?} ",j); } else if x!= 4.5 { println!("x= {:?} ",x); } else { println!("else"); } println!("exru5.ru finished"); } Execution output: exru5.ru running j= 3 x= 1.0 else exru5.ru finished 6. You need to be able to create functions, procedures, subroutines. exru6.ru source code // exru6.ru create and use functions and procedures fn factorial(i: u64) -> u64 { match i { 0 => 1, n => n * factorial(n-1) } } // end factorial fn sort(n:usize, mut _a:Vec) -> Vec { println!("sort n= {:?}",n); println!("a= {:?}",_a); let mut b = vec![0.0_f64; n]; for i in 0..n { b[i] = _a[i]; } for i in 0..(n-1) { let mut k = i; for j in (i+1)..n { if b[j] < b[k] { k = j; } if k != i { let t = b[i]; b[i] = b[k]; b[k] = t; } } } return b; } // end sort fn main() { println!("exru6.ru running"); let result = factorial(10); println!("factorial(10)= {}",result); let n = 10; let mut x = vec![0.0_f64; n]; let mut xs = vec![0.0_f64; n]; println!("sort vector of double, n= {}", n); for i in 0..n { x[i] = (n as f64)-(i as f64); print!("x[{}",i); println!("]= {:?}", x[i]); } xs = sort(n,x); println!("result vector"); for i in 0..n { print!("xs[{}",i); println!("]= {:?}", xs[i]); } println!(" "); println!("exru6.ru finished"); } Execution output: exru6.ru running factorial(10)= 3628800 sort vector of double, n= 10 x[0]= 10.0 x[1]= 9.0 x[2]= 8.0 x[3]= 7.0 x[4]= 6.0 x[5]= 5.0 x[6]= 4.0 x[7]= 3.0 x[8]= 2.0 x[9]= 1.0 sort n= 10 a= [10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0] result vector xs[0]= 1.0 xs[1]= 2.0 xs[2]= 3.0 xs[3]= 4.0 xs[4]= 5.0 xs[5]= 6.0 xs[6]= 7.0 xs[7]= 8.0 xs[8]= 9.0 xs[9]= 10.0 exru6.ru finished 7. You need to be able to read and write files in various formats. exru7.ru source code // exru7.ru write and read text files use std::io::Write; // for write use std::io::Read; // for first read use std::fs; // for second read fn main() // write 3 lines to out.txt, then read out.txt { println!("exru7.ru running"); // write 3 lines to out.txt let mut file_ref = std::fs::File::create("out.txt").expect("create failed"); file_ref.write_all("Hello World\n".as_bytes()).expect("write failed"); file_ref.write_all("Hello India\n".as_bytes()).expect("write failed"); file_ref.write_all("last line 3\n".as_bytes()).expect("write failed"); println!("Text written into file out.txt"); // first read out.txt let mut file_ref = std::fs::File::open("out.txt").unwrap(); let mut data = String::new(); file_ref.read_to_string(&mut data).unwrap(); println!("read data= {:?}",data); // second read out.txt let file_name = "out.txt"; let file_content = fs::read_to_string(file_name) .expect("Failed to read the file"); println!("from second read: "); println!("out.txt= {:?}",file_content); println!(" "); println!("\nFile contents:\n-----------\n{}\n", file_content); println!("exru7.ru finished"); } Execution output: exru7.ru running Text written into file out.txt read data= "Hello World\nHello India\nlast line 3\n" from second read: out.txt= "Hello World\nHello India\nlast line 3\n" File contents: ----------- Hello World Hello India last line 3 exru7.ru finished 8. You need to be able to use a number of files combined to build a program. This may include packages, libraries, operating system commands, header files, etc. exru8.ru source code Execution output: Other sample source code and output sin, cos, ..., exp, log, min, max import 'rust:math'; trig.ru source code trig_ru.out output test_string.ru source code test_string_ru.out output factorial.ru source code factorial_ru.out output

Last updated 2/14/2022