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
/^$/.test(''); // true | |
/$^/.test(''); // true |
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
// Using `s` flag to match all chars | |
// including \n: | |
const allCharsRe = /.+/s; | |
console.log(allCharsRe.test('\n')); // true |
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
// Using named capturing groups: | |
const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/; | |
const result = re.exec('2017-04-24'); | |
// NOTE: need runtime support to access `groups`, | |
// see `useRuntime` option: | |
console.log(result.groups.year); // 2017 |
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
// Using `x` flag with `RegExp`: | |
new RegExp(` | |
# A regular expression for date. | |
(?<year>\\d{4})- # year part of a date | |
(?<month>\\d{2})- # month part of a date | |
(?<day>\\d{2}) # day part of a date | |
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
// Using `x` flag with `re` shorthand | |
// Meta-chars like \d can be escaped using single slash. | |
// Plugin should specify `useRe` option. | |
re`/ | |
# A regular expression for date. | |
(?<year>\d{4})- # year part of a date | |
(?<month>\d{2})- # month part of a date |
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
/** | |
* Simple and educational `malloc` implementation. | |
* | |
* Dmitry Soshnikov <[email protected]> | |
* | |
* Maintains explicit linked list of allocated memory blocks. Each block | |
* has a header, containing meta-information, such as whether a block is | |
* free, its size, and a reference to the next block. | |
* | |
* Homework assignments: |
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
// https://godbolt.org/g/2xOK3B | |
int foo(int x) { | |
int y = 20; | |
return x + y; | |
} | |
int main() { | |
foo(10); | |
return 0; |
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
macro_rules! zoom_and_enhance { | |
(struct $name:ident { $($fname:ident : $ftype:ty),* }) => { | |
struct $name { | |
$($fname : $ftype),* | |
} | |
impl $name { | |
fn field_names() -> &'static [&'static str] { | |
static NAMES: &'static [&'static str] = &[$(stringify!($fname)),*]; | |
NAMES |
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
macro_rules! map( | |
{ $($key:expr => $value:expr),+ } => { | |
{ | |
let mut m = ::std::collections::HashMap::new(); | |
$( | |
m.insert($key, $value); | |
)+ | |
m | |
} | |
}; |
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::fmt::Debug; | |
use std::any::Any; | |
// Logger function for any type that implements Debug. | |
fn log<T: Any + Debug>(value: &T) { | |
let value_any = value as &Any; | |
// try to convert our value to a String. If successful, we want to | |
// output the String's length as well as its value. If not, it's a | |
// different type: just print it out unadorned. |