Rust development notes
March 31, 2026, 1:06 p.m.
This is a place for me to put my rust language based notes.
https://doc.rust-lang.org/book/
Compile : rustc main.rs (or chosen file name)
$ : ./main
use std::io;
use std::io::Write;
fn main() {
println!("testing string\n");
io::stdout().flush().unwrap();
}
Storing values inside a variable :
// Variables are immutable
fn main() {
let x = 5;
println!("The value of x is: {x}");
}
// mut annotation allows mutability
fn main() {
let mut x = 5;
println!("The value of x is: {x}");
x = 6;
println!("The value of x is: {x}");
}
$ :
The value of x is: 5
The value of x is: 6
Constants :
// constants are just that constant
// cant use mut with constants
// convention use upper case and underscore
fn main() {
const TWENTY_BUT_TWICE: u32 = 20 * 2;
println!("{}", TWENTY_BUT_TWICE)
}
Intergers , floating-point numbers, booleans, characters
number without fractional components.
Signed : need sign as the number could be negative number
Unsigned : will only be represented as a positive number
i8,u8,i16,u16,i32,u32, i64, u64,i128,u128, isize, usize
let f : bool = false // sets f as boolean type with value of false
can group multiple values into one type . 2 primitive compund types tuple and Array
general way of grouping a number of values with different types ; fixed length cant grow or get smaller once declared.
fn main() {
let temps: (u32, i8) = (20, -8);
println!("{:?}", temps)
}
collection of multiple values must be same type
// writing an array with type and amount of elements inside the array
fn main() {
let a: [u32; 3] = [5, 10, 12];
println!("{:?}", a)
}
// Accessing value of an array's index
// Need this library to write to
use std::io;
fn main() {
let a = [
"egg", "apple", "banana", "mango", "jalap", "coconut", " kimshi", "sake", "tacos",
"burritos",
];
println!("\n");
println!("list = {:?}", a);
println!("\n");
println!("What index's value do you want to see?, out of bounds index will error out");
let mut index = String::new();
// input number here
io::stdin()
.read_line(&mut index)
.expect("A whole number fool");
let index: usize = index.trim().parse().expect("not a nubmer");
let element = a[index];
println!("value : {element}");
}
// Adding to an array ; every element of an array must have the same type ;
// arrays in Rust have a fixed length.
// Need this library to write to
use std::io;
fn main() {
// other ways to write an array
// let a: [i32; 5] = ["jerry", "joe", "jack", "josephine", "joshelyn"];
// let a = [3; 5];
// make it read ["jerry", "joe", "jack", "josephine", "john"]
let mut a = ["jerry", "joe", "jack", "josephine", "joshelyn"];
println!("{:?}", a);
// user input
let mut number = String::new();
io::stdin()
.read_line(&mut number)
.expect("Failed to read line");
// place user input on array at last position
a[4] = &number;
println!("{:?}", a);
}
// /n may need to be trimmed
$ : ["jerry", "joe", "jack", "josephine", "johny\n"]
Adding to an Array with newline trimmed from array :
// Adding to an array ; every element of an array must have the same type ;
// arrays in Rust have a fixed length.
// Need this library to write to
// std = crate ; io = module ;
use std::io;
fn main() {
// other ways to write an array
// let a: [i32; 5] = ["jerry", "joe", "jack", "josephine", "joshelyn"];
// let a = [3; 5];
// make it read ["jerry", "joe", "jack", "josephine", "john"]
let mut a = ["jerry", "joe", "jack", "josephine", "joshelyn"];
println!("{:?}", a);
// user input
let mut empty_string = String::new();
// function
io::stdin()
.read_line(&mut empty_string)
.expect("Failed to read line");
// place user input on array at last position
// here the trim() removes spaces, whitespace characters like newlines and tabs.
a[4] = &empty_string.trim();
// https://doc.rust-lang.org/std/fmt/index.html
println!("{:?}", a);
}
// /n is trimmed in this example
$ : ["jerry", "joe", "jack", "josephine", "john"]