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
struct RGB{ | |
r:u8, | |
g:u8, | |
b:u8 | |
} | |
impl RGB{ | |
fn new()->Self{ | |
Self{ | |
r: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! max { | |
($x:expr) => ( $x ); | |
($x:expr, $($xs:expr),+) => { | |
{ | |
use std::cmp::max; | |
max($x, max!( $($xs),+ )) | |
} | |
}; | |
} |
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::io::{BufReader,BufWriter,BufRead,Read,Write}; | |
use std::net::{TcpListener, TcpStream}; | |
use std::fs::File; | |
use std::io::prelude::*; | |
// WriteトレイトとReadトレイトを実装しているSugoiTraitを実装すると | |
trait SugoiTrait:Write+Read{ | |
} | |
// TCPストリームも | |
impl SugoiTrait for std::net::TcpStream{ |
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::io; | |
use std::fmt::Display; | |
use std::fmt::Formatter; | |
// オレオレエラー列挙体 | |
#[derive(Debug)] | |
enum myError{ | |
RustError(io::Error), | |
FileError(myFileError), | |
SocketError(mySocketError) |
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::fs::File; | |
use std::io::prelude::*; | |
fn main(){ | |
let mut file = File::open("src/main.rs").unwrap(); | |
// ベクタを使ってread_to_endしているのはヒープに直接ぶち込みたいから。 | |
// スタックに制約がない場合は、普通の配列を初期化してでもOK | |
// 5回繰り返す。 | |
for i in 0..5{ | |
let mut buf = Vec::<u8>::new(); |
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
// 既存の型への自作trait以外の実装については | |
// newtypeパターンでないと実装できないらしい | |
// いろいろな制約があるとのこと、このあたりは知らん。 | |
use std::fs::File; | |
use std::io::prelude::*; | |
use std::fmt; | |
use std::fmt::Formatter; | |
// 型を新しく定義する(newtype) |
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
#[derive(Debug)] | |
struct FizzBuzzData{ | |
num:u32, | |
id:&'static str | |
} | |
#[derive(Debug)] | |
struct FizzBuzzN{ | |
data:Vec<FizzBuzzData>, | |
} |
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::io::stdin; | |
use std::io::Write; | |
use std::str::FromStr; | |
use std::io::Read; | |
use std::fmt::{Debug, Display}; | |
use std::error::Error; | |
#[derive(Debug)] | |
struct drink<'a>{ | |
price:u32, |
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
fn main(){ | |
let n = (0..20).collect::<Vec<u32>>(); | |
let v = n.iter().filter(|n|(*n)%2==1).map(|n|n*2).collect::<Vec<u32>>(); | |
let fb = n.iter().map(fizzbuzz).collect::<Vec<String>>(); | |
println!("{:?}",v); | |
println!("{:?}",fb); | |
//もっとスマートに書くと | |
println!("{:?}",(0..20).filter(|n|(*n)%2==1).map(|n| n * n).collect::<Vec<u32>>()); |
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
extern crate rand; | |
use rand::Rng; | |
fn main() { | |
let mut vec=(0x20..0x7f) | |
.filter_map(|n|std::char::from_u32(n as u32)) | |
.collect::<Vec<char>>(); | |
rand::thread_rng().shuffle(&mut vec); | |
vec.iter().for_each(|n|print!("{}",n)); | |
} |