Python and Rust
파이썬 그리고 러스트
Jihun
• 아직 배울 점이 많은 개발자 입니다.
DIVE INTO DIVERSITY
다양성에 빠지다
Introduce Rust
러스트 소개
Hello world
fn main() {

println!("Hello, world!");


}
Variable and mutability
fn main() {
let x = 5;

println!("The value of x is: {}", x);
}
fn main() {



let x = 5;
println!("The value of x is: {}", x);
x = 6;
println!("The value of x is: {}", x);
}
OOPS!
error[E0384]: cannot assign twice to immutable variable
`x`
--> src/main.rs:6:5
|
2 | let x = 5;
| - first assignment to `x`
...
6 | x = 6;
| ^^^^^ cannot assign twice to immutable variable
fn main() {
let mut x = 5;
println!("The value of x is: {}", x);
x = 6;
println!("The value of x is: {}", x);
}
The value of x is: 5

The value of x is: 6
Data type
Length Signed Unsigned
8-bit i8 u8
16-bit i16 u16
32-bit i32 u32
64-bit i64 u64
arch isize usize
Number literals Example
Decimal 98_222
Hex 0xff
Octal 0o77
Binary 0b1111_0000
Byte (u8 only) b'A'
fn main() {
let x = 2.0; // f64
let y: f32 = 3.0; // f32
let z = 2; // i32
let dc: i32 = 93_222; // i32
let t = true;
let f: bool = false;
}
Ownership
fn main() {
let s1 = String::from("hello");
}
Ownership
fn main() {
let s1 = String::from("hello");
let s2 = s1;
}
Ownership
fn main() {
let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1);
}
OOPS!
Slice
fn main() {
let s = String::from("hello world");
let hello = &s[0..5];
let world = &s[6..11];
}
fn main() {
let s = String::from("hello world");
let hello = &s[0..5];
let world = &s[6..11];
}
fn main() {
let s = String::from("hello");
let len = s.len();
let slice = &s[3..len];
let slice = &s[3..];
}
Iterators
fn main() {
for x in 0..10 {
println!("{}", x); // x: i32
}
}
Iterators
fn main() {
let arr = [1, 2, 3, 4, 5];
for v in arr.iter() {
println!("{}", v);
}
}
Match
fn main() {
let x = 5;
match x {
1 => println!("one"),
2 => println!("two"),
3 => println!("three"),
4 => println!("four"),
5 => println!("five"),
_ => println!("something else"),
}
}
function
fn main() {
print_number(5);
}

fn print_number(x: i32) {
println!("x is: {}", x);
}
function
fn main() {
let add = add_one(5);
println!("{}", add);
}
fn add_one(x: i32) -> i32 {
x + 1
} 결과: 6
Cargo
• 패키지 매니저

• 코드 빌드

• 다운로드 및 의존성 관리

• 다운로드한 패키지를 작성한 코드와 함께 빌드

• 각종 커맨드 제공 (test 등등 . .)
Pure python and
proof of work
algorithm
순수 파이선 그리고 증명 알고리즘
block.nonce = 0
computed_hash = block.compute_hash()
while not computed_hash.startswith('0' *BlockChain.difficulty):
block.nonce += 1
computed_hash = block.compute_hash()
print(computed_hash, block.nonce)
return computed_hash
Proof of work
“0” * 6
“000000”
difficulty
sha256을 했을때 앞에 “000000” 이 나와야합니다.
난이도에 따라서, 많은 컴퓨팅 파워를 필요하게 됩니다.
00000005efd98a9ed95f36a568632fa73a58912f33ddda23d41205ca8b2bf44c
Difficulty = 7
00e23adbc0a8c69c24b0f8382ba088733a21415bd3053d7cecb31a99a0779db2
Difficulty = 2
0ae23adbc0a8c69c24b0f8382ba088733a21415bd3053d7cecb31a99a0779db2
Difficulty = 1
0000000000000005efd98a9ed951f36a506863=1f33ddda23d41205ca8b2bf44c
Difficulty =15
block.nonce = 0
computed_hash = block.compute_hash()
while not computed_hash.startswith('0' *BlockChain.difficulty):
block.nonce += 1
computed_hash = block.compute_hash()
print(computed_hash, block.nonce)
return computed_hash
Proof of work
Problem
문제점
Let’s benchmark
Q / A

Python and rust 2018 pythonkorea jihun