Rust는 기본적으로 UTF-8 중심으로 문자열을 처리한다1. 그러나 MS-Windows는 euc-kr을 확장한 CP949(MS949)와 Unicode로 한글을 처리하므로2 MS-Windows에서는 영문, 숫자와 별개로 한글을 byte 단위로 처리할 때 2byte 또는 3byte의 문자가 혼재할 수 있다. 아래의 예제는 2byte (AnsiString) 한글만으로 Hex를 encode, decode 필요가 있을 때 참고할 만한 소스이다.
[dependencies]
encoding = "0.2.33"
hex = "0.4.2"
use encoding::{label::encoding_from_whatwg_label, EncoderTrap, DecoderTrap};
use hex;
fn main() {
let kor_eng_num = String::from("가나다 ABC 123");
println!("{}", kor_eng_num);
println!("---------------------");
let mut vec = Vec::new();
let string_list = kor_eng_num.split_whitespace();
for s in string_list {
println!("UTF8: {} / MS949: {}", hex::encode(s).to_uppercase(), split_hex(s.into()));
vec.push(split_hex(s.into())); // s.into() -> &s
}
println!("---------------------");
for hex_str in vec.iter() {
println!("{} : {}", hex_str, hex2str(hex_str));
}
}
Rvalue reference는 C++11에서 처음 소개된 기능으로 다소 이해하기 어려운 구조1로 되어있다. Each C++ expression (an operator with its operands, a literal, a variable name, etc.) is characterized by two independent properties: a type and a value category. Each expression has some non-reference type, and each expression belongs to exactly one of the three primary value categories: prvalue, xvalue, and lvalue2.
C++의 RValue 참조 및 Move 생성자 개념을 간단한 예제3를 통하여 정리해보았다.
Rust 프로그래밍에서 concurrency 모델의 핵심 메커니즘을 비교적 간단한 소스 코드를 통하여 단계별로 정리해보았다1. Go(lang)에서 goroutine, channel을 사용하여 함수와 메소드의 동시성을 구현할 수 있게 해주는 것2처럼 Rust에서는 ‘Concurrency, Threads, Channels, Mutex and Arc’로 동시성을 구현할 수 있다.
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
use std::time::Duration;
fn main() {
let v = vec![1, 2, 3];
let handle = thread::spawn(move || {
println!("vector: {:?}", v);
});
// - error : value borrowed here after move
// println!("{:?}", v);
// - channel을 이용하여 해결
handle.join().unwrap();
}