This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::alloc::{alloc, dealloc, Layout}; | |
//use std::fmt::Pointer; | |
fn main() -> () { | |
// Number of elements to allocate | |
const NUM_ELEMENTS: usize = 100; | |
// Size of each element in bytes | |
const ELEMENT_SIZE: usize = 8; // Assuming 4 bytes per element (e.g., 32-bit integer) | |
// Calculate total size in bytes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::alloc::{alloc, dealloc, Layout}; | |
//use std::fmt::Pointer; | |
fn main() { | |
// Number of elements to allocate | |
const NUM_ELEMENTS: usize = 100; | |
// Size of each element in bytes | |
const ELEMENT_SIZE: usize = 4; // Assuming 4 bytes per element (e.g., 32-bit integer) | |
// Calculate total size in bytes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#[allow(dead_code)] | |
enum Point { | |
Nothing, | |
TuplePoint(i32, i32), | |
StructPoint { | |
x: i32, | |
y: i32 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let mut sum: u64 = 0; | |
for x in 0..=32 { | |
// Calculate the term inside the summation | |
let term: f64 = (50.0 * 1e8) / (2.0_f64.powi(x as i32)); | |
println!("{} {}", x, term.to_string()); | |
// Take the floor of the result | |
let floor_term: u64 = term.floor() as u64; | |
// Add it to the sum | |
sum += floor_term; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::path::PathBuf; | |
use clap::{arg, command, value_parser, ArgAction, Command}; | |
fn main() { | |
let matches = command!() // requires `cargo` feature | |
.arg(arg!([name] "Optional name to operate on")) | |
.arg( | |
arg!( | |
-c --config <FILE> "Sets a custom config file" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use futures::future::ready; | |
use std::future::Future; | |
async fn future_test() -> String { | |
"test".to_string() | |
} | |
fn future_test2() -> impl Future<Output=String> { | |
ready("test2".to_string()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::f64::consts::PI; | |
fn main() { | |
let mut sum = 0.0; | |
let mut term = 1.0; | |
let mut k = 0; | |
loop { | |
sum += term; | |
k += 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn foo(slice: &[u8]) -> (&str, Option<&[u8]>) { | |
let mut iter = slice.utf8_chunks(); | |
// Annoying behavior of this iterator | |
let Some(chunk) = iter.next() else { | |
return (std::str::from_utf8(slice).unwrap(), None) | |
}; | |
let head = chunk.valid(); | |
if head.len() < slice.len() { | |
(head, Some(&slice[head.len()..])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use std::hash::{Hash, Hasher}; | |
fn main() { | |
let mut counts = HashMap::new(); | |
for i in 0..=144 { | |
let hash = hash_value(&i); | |
println!("{}",hash); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use std::hash::{Hash, Hasher}; | |
fn main() { | |
let mut counts = HashMap::new(); | |
for i in 0..=144 { | |
let hash = hash_value(&i); | |
*counts.entry(hash).or_insert(0) += 1; |