Skip to content

Instantly share code, notes, and snippets.

The Rust Playground rust-play

Block or report user

Report or block rust-play

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@rust-play
rust-play / playground.rs
Created Nov 11, 2019
Code shared from the Rust Playground
View playground.rs
struct Foo(Vec<u32>);
impl Foo {
fn map(&mut self) {
for item in self.0.iter_mut() {
*item = self.change();
}
}
fn change(&self) -> u32 {
@rust-play
rust-play / playground.rs
Created Nov 11, 2019
Code shared from the Rust Playground
View playground.rs
#![feature(const_generics)]
use std::cell::{Cell, UnsafeCell};
use core::mem::MaybeUninit;
fn main() {
let mut arr = [""];
let view = MutView::<_, 1>::new(&mut arr);
let a = "undefined".to_string();
@rust-play
rust-play / playground.rs
Created Nov 11, 2019
Code shared from the Rust Playground
View playground.rs
fn main(){
let mut x = 0;
let r = &mut x;
let p = r as *mut _;
*r = 4;
unsafe{
*p += 1;
}
*r = 3;
println!("{}", x);
@rust-play
rust-play / playground.rs
Created Nov 11, 2019
Code shared from the Rust Playground
View playground.rs
#![feature(const_generics)]
use std::cell::{Cell, UnsafeCell};
use core::mem::MaybeUninit;
fn main() {
let mut arr = [""];
let view = MutView::<_,_, 1>::new(&mut arr);
let a = "undefined".to_string();
@rust-play
rust-play / playground.rs
Created Nov 11, 2019
Code shared from the Rust Playground
View playground.rs
pub enum Index<'a> {
S(&'a str),
I(usize),
}
impl<'a> From<&'a str> for Index<'a> {
fn from(s: &'a str) -> Self {
Self::S(s)
}
}
@rust-play
rust-play / playground.rs
Created Nov 11, 2019
Code shared from the Rust Playground
View playground.rs
fn largest<T>(list: &[T]) -> T {
let mut largest = list[0];
for &item in list.iter() {
if item > largest {
largest = item;
}
}
largest
@rust-play
rust-play / playground.rs
Created Nov 11, 2019
Code shared from the Rust Playground
View playground.rs
//trace_macros!(true);
macro_rules! defmacro {
($name:ident, $foo:tt => $bar:tt) => {
defmacro!(@helper ($) $name, $foo => $bar);
};
(@helper ($dol:tt) $name:ident, $foo:tt => $bar:tt) => {
macro_rules! $name {
(r#$ ($foo)*) => ($dol ($bar)*)
}
}
@rust-play
rust-play / playground.rs
Created Nov 11, 2019
Code shared from the Rust Playground
View playground.rs
struct Interleave<I, T> where I: Iterator<Item = T> {
outer: Box<dyn Iterator<Item = (usize, I)>>,
skip: Vec<usize>,
}
impl<I, T> Interleave<I, T> where I: Iterator<Item = T> {
pub fn new(iter: impl Iterator<Item = I> + Clone + 'static) -> Self {
Self {
outer: Box::new(iter.enumerate().cycle()) as Box<dyn Iterator<Item = (usize, I)>>,
skip: Vec::new()
@rust-play
rust-play / playground.rs
Created Nov 11, 2019
Code shared from the Rust Playground
View playground.rs
fn main() {
let mut x = 41;
x ^= 1;
for i in 0..(std::mem::size_of::<usize>()*8) {
if x & (1 << i) == 0 { x ^= 1 << (i+1); } else { break; }
}
println!("{}", x);
}
@rust-play
rust-play / playground.rs
Created Nov 11, 2019
Code shared from the Rust Playground
View playground.rs
#![feature(const_generics)]
use std::cell::{Cell, UnsafeCell};
fn main() {
let mut arr = [""];
let view = MutView::new(&mut arr);
let a = "undefined".to_string();
*view.get_mut(0).unwrap() = a.as_str();
println!("{:?}", arr); // use after free
You can’t perform that action at this time.