Skip to content

Instantly share code, notes, and snippets.

@NebulaFox
NebulaFox / main.dart
Created October 7, 2020 04:20
TextField does not behave correctly
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
@NebulaFox
NebulaFox / main.dart
Last active October 4, 2020 19:32
Showing ToggleButtons does not remove border
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Toggle Button',
@NebulaFox
NebulaFox / playground.rs
Last active March 20, 2020 18:17
lazy loading problem
struct Context {
foo: Option<String>,
bar: Option<String>
}
impl Context {
fn new() -> Self {
Context {
foo: None,
bar: None
@NebulaFox
NebulaFox / playground.rs
Last active March 20, 2020 18:16
lazy loading solution
struct Context {
foo: String,
bar: String
}
struct ContextBuilder {
foo: Option<String>,
bar: Option<String>
}
@NebulaFox
NebulaFox / playground.rs
Created March 20, 2020 03:40
variable decleration
fn convert<S>(option: Option<S>) -> Option<String>
where
S: Into<String>,
{
match option {
Some(string) => Some(string.into()),
None => None
}
}
@NebulaFox
NebulaFox / playground.rs
Created March 20, 2020 02:59
strings wrapped up in Option
use rand; // 0.7.3
use rand::Rng;
fn make_a_string_with_optional<S>(option: Option<S>) -> String
where
S: AsRef<str>,
{
let mut s = String::from("Hello");
if let Some(string) = option {
s.push_str(" ");
@NebulaFox
NebulaFox / playground.rs
Created March 20, 2020 01:12
Wrap type implementing AsRef<str>
use rand; // 0.7.3
use rand::Rng;
#[derive(Debug, PartialEq)]
struct Wrap<T> {
inside: T
}
impl<T> Wrap<T> {
fn new(inside: T) -> Wrap<T> {
@NebulaFox
NebulaFox / playground.rs
Created March 20, 2020 01:01
Wrap type implementing Into<String>
use rand; // 0.7.3
use rand::Rng;
#[derive(Debug, PartialEq)]
struct Wrap<T> {
inside: T
}
impl<T> Wrap<T> {
fn new(inside: T) -> Wrap<T> {
@NebulaFox
NebulaFox / playground.rs
Last active March 19, 2020 19:36
trait magic
use rand; // 0.7.3
use rand::Rng;
fn make_a_string_with(string: &str) -> String {
let mut s = String::new();
s.push_str(string);
let mut rng = rand::thread_rng();
let r = rng.gen_range(0, 100);
for _ in 0..r {
@NebulaFox
NebulaFox / playground.rs
Created March 18, 2020 19:30
Unify HashSet contains and Vec contains solutions
#[allow(dead_code)]
fn contains<'a, V, T, Q>(arr: V, value: &Q) -> bool
where
T: 'a + std::cmp::PartialEq<Q>,
V: std::iter::IntoIterator<Item = &'a T>
{
arr.into_iter().any(|v| v == value)
}