Rust: Generics

Advertisements

Rust: Some important traits

std::fmt::Display

pub trait Display {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error>;
}

User-facing;

cannot be derived.

std::fmt::Debug

pub trait Debug {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error>;
}

Programmer-facing debug info;

can be used with #[derive] if all fields implement Debug

Debug: derived example from doc

#[derive(Debug)]
struct Point {
    x: i32,
    y: i32,
}

let origin = Point { x: 0, y: 0 };

println!("The origin is: {:?}", origin);

std::clone::Clone

Explicit duplication;

provide any type-specific behavior necessary to duplicate values safely.

pub trait Clone {
    fn clone(&self) -> Self;

    fn clone_from(&mut self, source: &Self) { ... }
}

std::marker::Copy

Implicit bit-wise duplication;

pub trait Copy: Clone { }

A type can implement Copy if all of its components implement Copy

 

Rust: Closures

When to use

We want to be able to specify some code in one place in our program, but then only execute that code if we actually need the result in some other place in our program.

An example from TRPL

let expensive_closure = |num| { 
    println!("calculating slowly..."); 
    thread::sleep(Duration::from_secs(2)); 
    num 
};

Note

  • If we try to use a different type with the same closure, we will get an error.

People’s views on myth

Ancient ideas on myth:

  • Anonymous: censorship
  • Plato: Myth constructs culture
  • Xenophanes: Culture constructs myth
  • Metrodorus and many others: allegory of physical environment
  • Aristarchus: Myth is only for literary use(anti-allegorical)
  • Euhemerus: Euhemerism(historical heroes symbolism)

Modern ideas (after Renaissance):

  • Bernard de Fontenelle:
    • Explanation to the physical world phenomena
  • David Hume:
    • Fearful human beings comfort themselves
  • Christian Gottlob Heyne:
    • Contextual resources for studies on ancient Greek and Roman culture/geography/etc.
    • Ancient people’s awe(wonder and fear) at nature
    • Concretization of abstract ideas
    • Creation of the word “myth”
  • Jonhann Gottfried Herder:
    • Innate expression of human beings
  • Walter Burkert (1931-):
    • Definition of myth:
      • Myth is a traditional tale told with secondary partial reference to something of collective importance, … told by someone for some reason

网易云音乐评论集

Collection of Netease Cloud Music comments

仁者见仁。

  • 看了评论,忽然觉得,网易云音乐的音乐评论将是个可怕的存在,如果它的服务一直存在。它将带未来的人穿越时空,去感受几十年前甚至更久以前人们听这首音乐的心情和故事。它不同于新闻的评论,老新闻看的人相对比较少,而音乐却几乎是不受时间约束的。社会和时代的缩影都会刻画在评论里。
  • 听了这么久的古典,终于明白只有大提琴才是钢琴的绝配。一个如医者的博学严谨,一个是老男人孩子气的浪漫;圆得一时任性,也能对得起一世芳华。
  • 风啊 它在漫山遍野扫荡 雪啊 它在天地之间疯狂 云中的月亮啊 请你避开它的锋芒 山间的生灵啊 不要与它对抗 今晚的我啊  瑟瑟中等待天亮 直到看见泉水解开冰封  花开引来蝴蝶 这是我向往的天堂  还是这个梦太漫长 待太阳点亮我的泪光。
  • 很奇怪,有时候你自己也描述不好的心情状态,听到一段旋律,好像就走进你的内心,莫名地就会觉得音乐读懂了你,或者也许是你借助音乐读懂了自己
  • 人们将自己的错误称之为经验,但真正的错误根本不会留下半点经验
  • 如果你死了,我愿意替你去死,但是你活着,我不愿意再看见你。我就想找一个人,一点也不像你。
  • 楼道声控灯你们怎么喊亮?我一般喊:“黑暗阻我前行,阴影笼罩大地,愿圣光为我引路,拥黎明伴我前进,十三层之光明真神,请赐予我力量,为我照亮前方,莹莹之光第九式——圣光降临!”
  • 为什么自己赞自己的时候没有通知呢,不然我也不会这么孤独了
  • 我能坐夜间火车保持清醒,我能画出小蓝魔尔浮蝶,我能做的都不是你要的,只是在我道谢的时候感觉到你的亲切带来的陌生疏离感,我唯有此时无力,我对此一切都无能为力,无数的泡泡,“砰”
  • Alice 世界上最孤独的鲸鱼 发出的频率是52赫兹 而正常鲸鱼是15至25赫兹 世上没任何鲸鱼能听懂它 它没有鱼群 没有同类 但它没放弃大海 从千里外的太平洋游到大西洋 纵然呐喊二十五年没人应答 但大海有多浩瀚它比别的鲸鱼都清楚
  • 梦想就像内裤,没必要逢人就露一下
  • 刚才做了个梦,梦到回到六年级的毕业考试的下午语文考试,我实在太困了,睡着了,醒来了却发现教室里一个人都没有了,却发现我最喜欢的数学老师站在讲台上,他走下来跟我说:醒醒,该回家。我一转脸发现身后全是熟悉却又叫不出名字的同学,瞬间醒来发现眼角泪水已经流到了枕巾
  • Vhiskey: 总有些歌,不至于加到喜欢的歌单,但也懒得为它们单独建一个歌单。还行,但没那么喜欢,然后又错过,不知道下次听是什么时候。
  • 今早真美啊 幸亏没死在昨天

  • 觌(dí)氅(chǎng)、餮(tiè)鼗(táo)、曩(nǎng)磲(qú)、蕤(ruí)颥(rú)、鳎(tǎ)鹕(hú)、鲦(tiáo)鲻 (zī)、耱(mò)貊(mò)、貘(mò)鍪(móu)、籴(dí)耋(dié)、瓞(dié)耵(dīng)
  • 时光商人i: 我试过销声匿迹,最终也无人问津。
  • 我的女朋友是个完美的女生,她不任性,不矫情,不粘人,不化妆,不存在
  • 摘下耳机, 我只不过是个落魄的哑巴 ——网易听众
  • 没有人会喜欢孤独,只是比起忽冷忽热,孤独让人感到踏实。
  • 既然你享受孤独,那又怕什么黑暗,如果不怕黑暗,为什么还畏惧死亡?
  • 百度问题有人问:大动脉在哪?有人答:你要干嘛?我爱你。别自杀。
  • 怎么区分孤独和寂寞,有天你走在大街上突然下雨了,你躲在屋檐下找遍了手机通讯录也不知道要找谁给你送伞,那一刻你是孤独的。而寂寞,是你一个人躲在屋檐下,安静的等雨停。
  • 我所有的自负皆来自我的自卑,所有的英雄气概都来自于我的软弱。嘴里振振有词是因为心里满是怀疑,深情是因为痛恨自己无情。这世界没有一件事情是虚空而生的,站在光里,背后就会有阴影,这深夜里一片寂静,是因为你还没有听见声音.
  • 民国时臧克家报考国立青岛大学,国文考试两题选一: 1、你为什么要报考国立青岛大学? 2、杂感。 臧克家选择第二题,全文是: 人生永远追逐着幻光,但谁把幻光看作幻光,谁便沉入无边的苦海。 写完,连标点算在内,不过30个字。 考官是闻一多,给了98分。
  • “你没有错,想做就做。你和我们下了不同的判断,是因为参考了不同的经验,做出选择,相信自己还是相信别人。我一直以来都弄不明白,为什么不管做了多么明智合理的选择,在结果出来之前谁都无法知道它的对错,到头来我们被允许做的,只是坚信那个选择,尽量不留下后悔而已。”———利威尔
  • 罗曼·罗兰有句名言,“世上只有一种英雄主义,就是在认清生活真相之后依然热爱生活。” 这种英雄主义最初级的表现形态就是——即使有被拒绝的可能,我依然要表达心中的想法。 不好意思,慢慢地就喜欢你了。 喜欢你就要告诉你,大不了连朋友都做不成,反正我不缺朋友,只缺你。
  • 喜欢你就要告诉你,大不了连朋友都做不成,反正我没有朋友。
  • 趁合租的女生在洗澡,我偷偷走进她房间,拿起她的衣服仔细的嗅了起来。正当我嗅得认真的时候,妹子突然进来了,问我:你在干什么?吓得我赶紧丢掉手里的衣服,把她逼到墙角,一个帅气的壁咚。妹子害羞的说:你…你想要怎么样?我深情注视她的双眼,大声道:你特么是不是偷我洗衣粉

Updated Saturday, May 6th, 2017


  • 那天哑巴说话了,他对着我们大喊了一句“都他妈给我安静”,然后又继续做他的哑巴
  • 今天你跟我说,以后不聊天了,你要好好学了,我说我没有你,我很难撑下去,你生气了,跟我说自己好好过,好自为之。那就好自为之吧,但是也不能不说一句话啊,你这样让我很担心的,你总认为我很假,可你不知道我说的句句是真,只是你不信,我不知道该做什么,只能求你别生气了,我走就走吧。

const and pointer in C++

Hello world!

This is the first post in my blog and also the first one under the topic of “C++”.

I am familiar with C and I noticed that C++ has more complex features and more widely usage. So the main purpose of the series of blogs related with C++ is to identify those advanced features(which C does not have) and remind me of them when they are needed.


Top-level const vs. Low-level const

Top-level const and low-level const have close relationships with pointers.

Supposed that a pointer in the codes points to another object, then,

Top-level const represents that the pointer is assigned to be a constant,

Low-level const represents that the object pointed by the pointer is assigned to be a constant.

int i = 0;
int *const p1 = &i;         // Top-level const, p1(the pointer) is assigned to be a constant
int j = 0;
const int *p2 = &j;         // Low-level const, the location of j(the object being pointed) is assigned to be a constant (although j is a variable)

Also, Under normal situations, Top-level const initialize any object with any data type in C++ as a constant.

const int c = 1024;         // Top-level const

If you are dealing with more complex ones.

int v = 9;
int *p = &v;
const int *const p3 = p;    // note: v and p are still assignable 

The left one is low-level const while the right one is top-level const.

p3 is a constant pointer to a constant location. No matter how you change p afterwards, p3 cannot be modified and it always points to the initial value of p (the initial location of v).

Note: The const expressions for references are low-level const.

int c = 10;
const int &d = c;           // Low-level const

Copying constants

There are some limits for low-level const when copying a constant.

No matter assigning to an object or being assigned by another object, both of them have to be assigned by low-level const expressions(The objects being pointed are constants), or the data types of them have to be convertable. Normally, A variable can be converted to a constant, but not the opposite.

A constant assigned by top-level const expression is able to assign to other objects(Of course, the constant cannot be assigned).

How auto & decltype() dealing with const expressions?

Definitions:

auto would neglect top-level const but retain low-level const.

In contrast, decltype() would retain both of them.

const int c = 10;
auto d = c; // the data type of d is <int>, not a constant
decltype(c) e = c;     // the data type of e is a constant <int>

Reference:

C++ Primer (5th Edition) by Stanley B. Lippman, Josée Lajoie, Barbara E. Moo