Кольцов Степан рассказал о Rust – современном, практичном, быстром и безопасном языке программирования.
- Basic examples
- fn main() { println!(«hello world»); } Hello world
- fn is_prime(n: uint) -> bool { range(2, n).all(|x| n % x != 0) // lambda
- Data types • primitives: bool, int, u32, f32, etc. • builtins: &[], &str • user-defined: struct, enum, trait • functions
- struct SocketAddr { host: String, port: uint, } struct
- enum CacheStatus { Error(String), Cached(uint), }
- fn create_server(conf: Conf) { … }
- fn create_server(conf: Conf) { … }
- Vec; &[T]; String, &str C++ Rust std::vector<T> std::Vec<T> std::array_view &[T] std::string std::String std::string_view &str
- // C++ llvm::ArrayRef<T>; Rust &[T] struct Slice<T> { T* begin; T* end;
- // similar to std::vector let v1: Vec<uint> = vec!(10, 20, 30)
- fn sum<T>(ns: &[T]) -> T { let sum = T::zero(); for n in ns { sum += n; } n }
- Pointers • raw unsafe pointers *Foo • borrowed pointers &Foo • smart pointers
- std::string get_url() { return «http://yandex.ru»; }
- get_scheme_from_url(string_view url) { unsigned colon = url.find(‘:’); return url.substr(0, colon); }
- fn get_url() -> String { «http://yandex.ru».to_string()
- struct UrlParts<‘a> { scheme: &’a str, host: &’a str, port: uint, path: &’a str, }
- enum MaybeOwned<‘a> { Slice(&’a str), Owned(String)
- impl<‘s> MaybeOwned { fn as_slice(&’s self) -> &’s str { match self { Owned(ref s) => s.as_slice(), Slice(s) => s, }
- struct Person { name: String
- fn longest_str<‘s>(a: &’s str, b: &’s str) -> &’s str { if a.len() > b.len() { a } else { b } }
- void foo(vector<int>& xs) { typedef vector<int>::iterator iter; for (iter i = xs.begin(); i
- fn foo(xs: &mut Vec<int>) { for p in xs.iter() { if *p == 0 { xs.push(1); } } } Mutability: Rust
- let mut a = 1; let b = &mut a; let c = &mut a;
- let mut a = 1; a = 2; let b = &a; a = 3
- Smart Pointers C++ Rust std::unique_ptr<T> Box<T> std::shared_ptr<T> Rc<T> or Arc<T>
- struct Foo { v: int, } ! let ptr = Rc::new(Foo { … }); println!(«{}», ptr.v); User-defined pointers: Rc<T>
- struct RcBox<T> { value: T, ref_count: uint, }
- Threads • tasks • channels • Arc • AtomicInt • Mutex
- for i in range(0, 100) { // proc is a keyword // proc closure can be passed btw threads // and may be called no more than once task::spawn(proc() { println!(«{}», i); }); } Tasks
- let (sender, receiver) = channel(); ! for i in range(0, 100) { let sender_for_task = sender.clone(); task::spawn(proc() { // do something sender_for_task.send(i * i); }); }
- // similar to Rc<T> except // Arc uses atomic counter, not plain // data is immutable inside Arc // so Arc can be safely shared between threads let conf = Arc::new(ServerConf { … })
- Mutex<T> • Mutex<T> = T + mutex • Safely share data between threads
- fn do_smth(shared_data: Arc<Mutex<T>>) { // guard + smart pointer let ptr_and_lock = shared_data.lock(); ptr_and_lock.foo_bar()
- unsafe fn memset(mem: *mut u8, c: u8, len: uint) { for i in range(0, len) { *mem.offset(i as int) = c; } }
- Program performance 0 25 50 75 100 Performance of compiled code C++ Rust Java
- Problems • compile-time metaprogramming • IDE • incremental compilation
- trait Natural { fn zero() -> Self; fn next(self) -> Self; }
- macro_rules! vec( ($($e:expr),*) => ({ let mut _temp = ::std::vec::Vec::new(); $(_temp.push($e);)* _temp }) )
- fn print_anything(xs: &[Box<ToStr>]) { for x in xs.iter() { println!(«{}», x.to_str()); } }
- class FileInputStream: public InputStream { int fd; }
- struct FileInputStream { fd: int } ! impl InputStream for FileInputStream { … }
- Fin Stepan Koltsov <nga@yandex-team.ru>