Rustコトハジメ

プログラミング言語Rustと競プロに関する情報をお届けします。

Language Spec

Copy trait and its applications

What is Copy trait What conditions should Copy trait satisfy The applications of Copy trait What is Copy trait Copy trait is a marker trait that claims the data is able to copy instead of move. So types that implement Copy trait have copy …

Trait tells everything by the type

What confused me when I was a beginner Traits are responsible for the controlling ownership and reference of types Trait tells everything by the type definition What confused me when I was a beginner When I started to learn Rust what was c…

Implement crazy math teacher in Rust

Teacher is the absolute Ordering of multiplication problem Let's write a joke program to emulate the crazy teacher Other problem: Human pyramid Teacher is the absolute Foreign people often say that Japanese people are obedient to rules: th…

The fact that closure's super traits are generated by compiler

In the previous post, we have learned that closure is generated by the compiler (e.g. The compiler generates a class implements FnMut for a closure that mutates the captured environment) but how super traits for the closure is generated wa…

Why do Iterator methods take FnMut instead of Fn?

Rust iterator supports functional programming that is, it supports functions like map and fold fn map<B, F>(self, f: F) -> Map<Self, F> where Self: Sized, F: FnMut(Self::Item) -> B, fn fold<B, F>(mut self, init: B, mut f: F) -> B where Self: Sized, F: FnMut(B,</b,></self,></b,>…

Smart pointers in Rust

www.rustforbeginners.com In the post above, I mentioned the Vec is a ownership pointer that has three members. In Rust, pointer-like stuffs which has internal special metadata are called smart pointers. Smart pointers in Rust are such as: …

Arc<Mutex<T>> is a design pattern in Rust

Rust has no GC and you can't write some kind of code that you ever wrote in other languages. The typical case is recursive data structures like list, tree and graph. By looking for documents or articles, you will find types like the follow…

How reference of integer is able to add

Are you surprised by this code? If you are a C/C++ programmer you should be. fn main() { let a = 5; let b = 3; println!("{},{},{},{}", a+b, a+&b, &a+b, &a+&b) } To surprise the output is 8,8,8,8 The trick behind this behavior is macros: Ar…

Learn a lot from Index trait

fn main() { let mut v = vec![1, 2, 3, 4, 5]; let a = &v[0]; v.push(6); } If you try to compile this code error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable --> prog.rs:4:5 | 3 | let a = &v[0]; | - immutable…

Closure in Rust is generated by the compiler

The three types of closure in Rust Mix immutable borrow and mutable borrow in closure Discussion TODO The three types of closure in Rust In Rust, compiler generates distinct class for each closure in code and they implements Fn or FnMut or…

Zero cost abstraction explained

The first time I heard the word "Zero cost abstraction" I had no idea what it is. In the same way I could not soon understand why GC is not needed while free is not needed at the same time, I was confused what zero cost of abstraction is. …

Into to From blanket implementation is not possible

In the previous post www.rustforbeginners.com we have confirmed that there is a blanket implementation impl Into for From but there is none for the opposite. In the post, I explained the reason as impl Into for From is more nice to have th…

Into implemented from From but not the opposite

From trait and Into trait are traits that takes an ownership of a object to make another object. They are not implicitly used by the compiler like Deref trait but widely exploited to write more generalized code as like AsRef trait. In Prog…

Learn rvalue from Cursor implementation

In this article, I will share a nice finding when I explore the Cursor implementation of Read trait. The implementation below means that anything which can lead &[u8] (AsRef<[u8]>) can be an instance of Read by wrapping it by Cursor. impl<T> </t>…

How pointers are placed in stack

In Rust, Vec has a data on the heap and the ownership pointer has three metadata (pointer, capacity and the length) and placed on the stack. Slice is a fat pointer with two members (pointer and length) and placed on the stack. And data wil…

Deref is not called before IntoIterator

In the previous post www.rustforbeginners.com we left a question. And this post is the answer. I will share the interesting finding from a simple experiment. The question Experiment Result Discussion The question The Rust library has imple…

IntoIterator implementations explored

pub trait IntoIterator { /// The type of the elements being iterated over. type Item; /// Which kind of iterator are we turning this into? type IntoIter: Iterator<Item=Self::Item>; fn into_iter(self) -> Self::IntoIter; } For-looping in Rust is like this: f</item=self::item>…

Explicit typing is required to get a fat pointer

impl<T> ops::Deref for Vec<T> { type Target = [T]; fn deref(&self) -> &[T] { unsafe { let p = self.buf.ptr(); assume(!p.is_null()); slice::from_raw_parts(p, self.len) } } } We know that Rust implicitly convert &Vec<T> to &[T] thanks to Deref trait.</t></t></t>…

Understand the reference resolution rule in Rust

There is a lot of implicit type conversions in Rust. Some of them are auto-referencing: Rust compiler add & or &mut automatically to the &self or &mut self in method call. auto-dereferencing: Rust compiler dereferences type by adding * unt…

Learn fat pointer from Read trait

Read trait is a trait that abstracts reading something from somewhere and the core method is the read. Let's see the type. pub trait Read { #[stable(feature = "rust1", since = "1.0.0")] fn read(&mut self, buf: &mut [u8]) -> Result<usize>; The imp</usize>…