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 Aug 22, 2019
Code shared from the Rust Playground
View playground.rs
fn main() {
println!("Hello, world!");
}
@rust-play
rust-play / playground.rs
Created Aug 22, 2019
Code shared from the Rust Playground
View playground.rs
#[derive(Debug, Clone, Copy)]
enum Foo {
A,
B,
C
}
fn main() {
let x = Foo::A;
match x {
@rust-play
rust-play / playground.rs
Created Aug 22, 2019
Code shared from the Rust Playground
View playground.rs
fn main() {
let s1 = String::from("hello");
let (s2, len) = calculate_length(s1);
println!("The length of '{}' is {}.", s2, len);
}
fn calculate_length(s: String) -> (String, usize) {
let length = s.len(); // len() returns the length of a String
@rust-play
rust-play / playground.rs
Created Aug 22, 2019
Code shared from the Rust Playground
View playground.rs
fn main() {
let s1 = gives_ownership(); // gives_ownership moves its return
// value into s1
let s2 = String::from("hello"); // s2 comes into scope
let s3 = takes_and_gives_back(s2); // s2 is moved into
// takes_and_gives_back, which also
// moves its return value into s3
} // Here, s3 goes out of scope and is dropped. s2 goes out of scope but was
@rust-play
rust-play / playground.rs
Created Aug 22, 2019
Code shared from the Rust Playground
View playground.rs
fn main() {
let s = String::from("hello"); // s comes into scope
takes_ownership(s); // s's value moves into the function...
// ... and so is no longer valid here
let x = 5; // x comes into scope
makes_copy(x); // x would move into the function,
// but i32 is Copy, so it’s okay to still
@rust-play
rust-play / playground.rs
Created Aug 22, 2019
Code shared from the Rust Playground
View playground.rs
pub type Payload<R> = (Role<R>, Target);
#[derive(Debug, Clone, Serialize, Deserialize)]
struct SGSecret(String);
impl Default for SGSecret {
fn default() -> Self{ Self(String::default()) }
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
@rust-play
rust-play / playground.rs
Created Aug 22, 2019
Code shared from the Rust Playground
View playground.rs
pub type Payload<R> = (Role<R>, Target);
#[derive(Debug, Clone, Serialize, Deserialize)]
struct SGSecret(String);
impl Default for SGSecret {
fn default() -> Self{ Self(String::default()) }
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
@rust-play
rust-play / playground.rs
Created Aug 22, 2019
Code shared from the Rust Playground
View playground.rs
use core::{
mem::zeroed,
ptr::{null, null_mut},
};
use winapi::{
shared::windef::HWND,
um::{
libloaderapi::GetModuleHandleW,
winuser::{
CreateWindowExW, DefWindowProcW, DispatchMessageW, GetMessageW, RegisterClassW,
@rust-play
rust-play / playground.rs
Created Aug 22, 2019
Code shared from the Rust Playground
View playground.rs
use std::mem::size_of;
use std::num::NonZeroI8;
fn main() {
println!("i8: {}, nonzero i8: {}", size_of::<Option<i8>>(), size_of::<Option<NonZeroI8>>());
println!("vec: {}, option: {}", size_of::<Vec<u8>>(), size_of::<Option<Vec<u8>>>());
}
@rust-play
rust-play / playground.rs
Created Aug 22, 2019
Code shared from the Rust Playground
View playground.rs
use std::{
sync::{Arc, Mutex},
thread,
};
struct Test {
flag: bool,
}
impl Test {
You can’t perform that action at this time.