Skip to content

Instantly share code, notes, and snippets.

View jcbritobr's full-sized avatar
😉

Júlio César de Brito Gardona jcbritobr

😉
View GitHub Profile
@jcbritobr
jcbritobr / string-conversion.rs
Created April 26, 2022 22:57 — forked from jimmychu0807/string-conversion.rs
Conversion between String, str, Vec<u8>, Vec<char> in Rust
use std::str;
fn main() {
// -- FROM: vec of chars --
let src1: Vec<char> = vec!['j','{','"','i','m','m','y','"','}'];
// to String
let string1: String = src1.iter().collect::<String>();
// to str
let str1: &str = &src1.iter().collect::<String>();
// to vec of byte
@jcbritobr
jcbritobr / gtkmultithreading.rs
Last active November 18, 2020 22:31
Gtk-rs multithreading with rust
#![windows_subsystem = "windows"]
use gtk::prelude::*;
use std::{thread, time::Duration};
fn main() {
println!("Hello, world!");
if gtk::init().is_err() {
eprintln!("failed to initialize");
return;
}
@jcbritobr
jcbritobr / xor_flux.jl
Created October 25, 2020 14:04
Julia flux simple multilayer perceptron to solve xor problem
using Flux
using Plots
x = [0f0 0 1 1; 0 1 0 1]
y = [0f0 1 1 0]
xornn_model = Chain(
Dense(2, 2, σ),
Dense(2, 1, σ)
)
@jcbritobr
jcbritobr / py_math.rs
Created October 18, 2020 14:02
Rust python module with pyo3
use pyo3::{
prelude::pyfunction, prelude::pymodule, types::PyModule, wrap_pyfunction, PyResult, Python,
};
#[pyfunction]
fn sum_numbers(a: usize, b: usize) -> PyResult<usize> {
Ok(a + b)
}
#[pymodule]
@jcbritobr
jcbritobr / cursive.rs
Created October 11, 2020 21:31
Cursive ui, iteration and mutation of values in rust
use cursive::{
self, traits::Boxable, traits::Nameable, views::Button, views::Dialog, views::DummyView,
views::EditView, views::LinearLayout, views::SelectView, Cursive,
};
fn main() {
let mut siv = cursive::default();
let select = SelectView::<String>::new()
.on_submit(on_submit)
.with_name("select")
@jcbritobr
jcbritobr / cursive_configuration_windows.toml
Created October 11, 2020 17:18
Cursive sample with generation and destruction of dialogs.
[package]
name = "cursive_test_windows"
version = "0.1.0"
authors = ["unknown <[email protected]>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies.cursive]
version = "0.15.0"
@jcbritobr
jcbritobr / linkedlist.rs
Created October 10, 2020 21:18
Linked list with tail in rust
use std::{cell::RefCell, rc::Rc};
type SingLink = Option<Rc<RefCell<Node>>>;
#[derive(Clone)]
struct Node {
value: String,
next: SingLink,
}
@jcbritobr
jcbritobr / readimplementation.rs
Created October 9, 2020 20:52
A buffer using Read trait implementation.
use std::{fmt::Display, io::BufReader, io::Read, io::Write, str};
struct MyBuffer {
buf: Vec<u8>,
off: usize,
}
impl MyBuffer {
fn new(data: &[u8]) -> MyBuffer {
MyBuffer {
@jcbritobr
jcbritobr / spcmc.rs
Created September 23, 2020 23:33
SPCMC(Single Producer, Multiple Consumer) pattern in Rust
use std::{sync::mpsc, sync::Arc, sync::Mutex, thread};
type Closure = Box<dyn FnOnce() + Send + 'static>;
const ERR_LOCK: &str = "cant acquire resource";
fn main() {
let (tx, rx) = mpsc::channel::<Closure>();
let rxtf = Arc::new(Mutex::new(rx));
@jcbritobr
jcbritobr / conprod.rs
Created September 23, 2020 20:44
Rust producer consumer pattern, sending functions between channels
use std::{sync::mpsc, thread};
type Closure = Box<dyn FnOnce() + Send + 'static>;
fn main() {
let (tx, rx) = mpsc::channel::<Closure>();
let handle = thread::spawn(move ||{
while let Ok(data) = rx.recv(){
data();
}