Skip to content

Instantly share code, notes, and snippets.

@vicradon
Last active June 2, 2023 05:05
Show Gist options
  • Save vicradon/e90195e72c22176b79efb33cf7795d40 to your computer and use it in GitHub Desktop.
Save vicradon/e90195e72c22176b79efb33cf7795d40 to your computer and use it in GitHub Desktop.
Rust snippets
/**
* This program finds the difference between two arrays.
* It essentially removes elements from the first array that are present in the second array.
*/
use std::collections::HashSet;
use std::hash::Hash;
use std::iter::FromIterator;
fn array_diff<T: PartialEq + std::clone::Clone>(a: Vec<T>, b: Vec<T>) -> Vec<T>
where
T: Eq,
T: Hash,
{
let mut result: Vec<T> = Vec::new();
let b: HashSet<T> = HashSet::from_iter(b.iter().cloned());
for num in a {
if !b.contains(&num) {
result.push(num);
}
}
result
}
fn main() {
println!("{:?}", array_diff(vec![1, 2, 2, 3, 5], vec![2, 4]));
}
fn main(){
let mut array: [u32; 3] = [4, 5, 2];
println!("{:?}", array);
array[1] = 17;
println!("{:?}", array);
let mut vector = vec!["0"];
vector.push("1");
println!("{:?}", vector);
}
fn main() {
let x = 5u32;
let y = {
let x_squared = x * x;
let x_cube = x_squared * x;
// This expression will be assigned to `y`
x_cube + x_squared + x
};
let z = {
// The semicolon suppresses this expression and `()` is assigned to `z`
2 * x
};
println!("{}", x + y + z);
println!("x={x} y={y} z={z}");
}
use std::fmt;
#[derive(Debug)]
struct Matrix(f32, f32, f32, f32);
// This trait allows us to format the display when we do println! using '{}'
impl fmt::Display for Matrix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "( {} {} )\n( {} {} )", self.0, self.1, self.2, self.3)
}
}
fn main() {
let matrix = Matrix(1.1, 1.2, 2.1, 2.2);
println!("{:?}", matrix);
println!("{}", matrix);
}
#[derive(Debug)]
struct Point {
x: f32,
y: f32,
}
struct Rectangle {
top_left: Point,
bottom_right: Point,
}
impl Rectangle {
fn rect_area(self) -> f32 {
return (self.bottom_right.x - self.top_left.x) *
(self.top_left.y - self.bottom_right.y);
}
}
fn main() {
let decimal = 65.4321_f32;
let integer = decimal as u8;
let character = integer as char;
println!("Casting: {} -> {} -> {}", decimal, integer, character);
println!("-100.0 as u32 is : {}", -100.0 as u32);
println!(" nan as u8 is : {}", f32::NAN as u8);
}
fn main() {
let n = 5;
if n < 0 {
print!("{} is negative", n);
} else if n > 0 {
print!("{} is positive", n);
} else {
print!("{} is zero", n);
}
let big_n =
if n < 10 && n > -10 {
println!(", and is a small number, increase ten-fold");
10 * n
} else {
println!(", and is a big number, halve the number");
n / 2
};
println!("{} -> {}", n, big_n);
}
/*
5 is positive, and is a small number, increase ten-fold
5 -> 50
NB: Every let expression should end with a semicolon.
You don't need a return statement with those expression blocks in the if and else
But their types must be same
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment