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
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
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
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
mod calc; | |
mod foo; | |
fn main() { | |
assert!(foo::bar()); | |
assert_eq!(2, calc::mul::mul(2, calc::mul::MUL_IDENTITY)); | |
println!("{}", calc::hello()); | |
} |
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
pub mod mul; | |
mod strings; // Hidden to the outside world | |
pub fn hello() -> &'static str { | |
strings::HELLO | |
} |
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
mod mul; // Now it's hidden too | |
pub use mul::mul; // But we can still access this function | |
mod strings; | |
pub fn hello() -> &'static str { | |
strings::HELLO | |
} |
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
pub trait Add<Rhs = Self> { | |
type Output; | |
fn add(self, rhs: Rhs) -> Self::Output; | |
} |
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
impl Add for Counter { | |
type Output = Counter; | |
fn add(self, rhs: Self) -> Self::Output { | |
Self(self.0 + rhs.0) | |
} | |
} |
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
impl Add for &Counter { | |
type Output = Counter; | |
fn add(self, rhs: Self) -> Self::Output { | |
Counter(self.0 + rhs.0) | |
} | |
} |