This file contains 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::{rc::Rc, cell::RefCell}; | |
macro_rules! nth { | |
($tuple:expr, $axis:tt) => { | |
match $axis { | |
0 => $tuple.0, | |
1 => $tuple.1, | |
_ => unreachable!() | |
} | |
} |
This file contains 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::{rc::Rc, cell::RefCell}; | |
struct XLimit { | |
vector: Rc<RefCell<(i32, i32)>>, | |
limit: i32 | |
} | |
impl XLimit { | |
fn limit(&self) { | |
if (*self.vector).borrow().0 > self.limit { |
This file contains 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
struct Foo { | |
bar: i32 | |
} | |
impl Foo { | |
fn instead_of_this() -> Foo { | |
Foo { bar: 0 } | |
} | |
fn you_can_use_this() -> Self { |
This file contains 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 vec = vec!["f", "o", "o"]; | |
// instead of this | |
for i in 0..vec.len() { | |
println!("The character at {i} is {}", vec[i]); | |
} | |
// you can use this | |
for (i, char) in vec.iter().enumerate() { | |
println!("The character at {i} is {char}"); | |
} |
This file contains 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
const TOKEN_AS_STR: &'static str = include_str!("./token.txt"); | |
const TOKEN_AS_BYTES: &[u8] = include_bytes!("./token.txt"); | |
fn main() { | |
assert_eq!("HelLoWoRLd", TOKEN_AS_STR); | |
assert_eq!("HelLoWoRLd".as_bytes(), TOKEN_AS_BYTES); | |
} |
This file contains 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
enum ReadingError { | |
OutOfBounds, | |
InvalidBuffer | |
} | |
trait InsteadOfThis { | |
fn next_char() -> Result<u8, ReadingError>; | |
fn next_line() -> Result<String, ReadingError>; | |
fn char_at(pos: usize) -> Result<u8, ReadingError>; | |
} |
This file contains 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 overflow(n: i32, max: i32) -> Option<i32> { | |
if n > max { | |
Some(n - max) | |
} else { | |
None | |
} | |
} | |
fn main() { | |
let n = 15; |
This file contains 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 sum<T>(numbers: &[T]) -> T | |
where | |
T: Add<Output = T> + Copy + Zero, | |
{ | |
let mut total: T = T::zero(); | |
for i in numbers.into_iter() { | |
total = total + *i; | |
} | |
total | |
} |
This file contains 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 sum<T>(numbers: &[T]) -> T | |
where | |
T: Add<Output = T> + Copy, | |
{ | |
let mut total = numbers[0]; | |
for i in numbers.into_iter().skip(1) { | |
total = total + *i; | |
} | |
total | |
} |
This file contains 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 sum<T>(numbers: &[T]) -> T { | |
let mut total = numbers[0]; | |
for i in numbers.into_iter().skip(1) { | |
total = total + *i; | |
} | |
total | |
} |