1. Functional Programming
2. Agenda
3. Recall: AddressBook
4. Borrowing
Reference to variable
5. Memory Safety
Correct handling of references
6. Dangling Pointers
Referring to a freed object
Reference to variable
Correct handling of references
Referring to a freed object
你好,我是悦创。今天我们一起来学习 Rust 中的枚举(enum)和模式匹配(pattern matching)。
枚举是 Rust 中非常重要的复合类型,也是最强大的复合类型之一,广泛用于属性配置、错误处理、分支流程、类型聚合等场景中。学习完这节课后,你会对 Rust 的地道风格有新的认识。
枚举是这样一种类型,它容纳选项的可能性,每一种可能的选项都是一个变体(variant)。Rust 中的枚举使用关键字 enum 定义,这点与 Java、C++ 都是一样的。与它们不同的是,Rust 中的枚举具有更强大的表达能力。
你好,我是悦创。今天我们来学习 Rust 中的复合类型——结构体。
结构体是由其他的基础类型或复合类型组成的,当它所有字段同时实例化后,就生成了这个结构体的实例。在 Rust 中,结构体使用 struct 关键字进行定义。
这节课我们会通过各种各样的示例来了解结构体,其中有部分示例来自于官方 The Book。我们一起来看一下。
下面我们先来看一下结构体示例,定义一个 User 结构体。
你好,我是悦创,今天我们来认识一下 Rust 中和我们打交道最频繁的朋友——字符串。
这节课我们把字符串单独拿出来讲,是因为字符串太常见了,甚至有些应用的主要工作就是处理字符串。比如 Web 开发、解析器等。而 Rust 里的字符串内容相比于其他语言来说还要多一些。是否熟练掌握 Rust 的字符串的使用,对 Rust 代码开发效率有很大影响,所以这节课我们就来重点攻克它。
我们在 Rust 里常常会见到一些字符串相关的内容,比如下面这些。
你好,我是悦创。今天我们继续探讨 Rust 中所有权这一关键设计。
上节课我们了解了计算机内存结构知识,理解了 Rust 在内存资源管理上特立独行的设计——所有权,也知道了 Rust 准备采用所有权来重构整个软件体系。那么这节课我们继续学习所有权的相关内容——借用与引用,学完这节课我们就会对 Rust 语言的所有权方案有一个相对完整的认知了。
这节课我会用一些精心设计的示例,让你体会 Rust 引用的独特之处。
我们来复习一下上一节课最后一个例子。我们想在函数 foo 执行后继续使用字符串 s1,我们通过把字符串的所有权转移出来,来达到我们的目的。
摘要
本文深入讲解了 Rust 语言中的所有权概念以及栈与堆的管理。首先介绍了 Rust 中的值分为固定尺寸和非固定尺寸两类,以及不同编程语言对数字类型的处理差异。随后,详细解释了栈和堆的概念,以及它们在程序中的作用和区别。文章还提到了栈帧与堆空间资源的关系,以及内存泄漏问题。最重要的是,文章详细阐述了 Rust 中的所有权概念,包括所有权的基础规则和作用域,以及通过具体例子展示了在 Rust 中变量赋值时的所有权转移特性。通过这些内容的讲解,读者可以更好地理解 Rust 语言中的资源管理和内存管理机制。文章的深入讲解有助于读者更全面地理解 Rust 语言的特性和设计理念。文章还提到了 Rust 中的所有权概念,包括所有权的基础规则和作用域,以及通过具体例子展示了在 Rust 中变量赋值时的所有权转移特性。通过这些内容的讲解,读者可以更好地理解 Rust 语言中的资源管理和内存管理机制。
你好,我是悦创。今天是我们的 Rust 入门与实战第一讲。
无论对人,还是对事儿,第一印象都很重要,Rust 也不例外。今天我们就来看一看 Rust 给人的第一印象是什么吧。其实 Rust 宣称的安全、高性能、无畏并发这些特点,初次接触的时候都是感受不到的。第一次能直观感受到的实际是下面这些东西。
你好,我是悦创。欢迎加入《Rust 语言从入门到实战》,开启一段全新的学习旅程。
2018 年我开始接触 Rust,到现在已经 6 年多了,从拥护者到布道者,一直在学习积累。
我以前是一个 C 极大主义者「在大学中时」,觉得用 C 语言就能完成我想做的任何事情。但后来 C 程序中的各种缓冲区溢出、段错误等问题一直困扰着我,不管我采用何种最佳实践集合,那些问题总是根除不了。
这也是我学习 Rust 的重要契机。那时我就有种直觉——它一定会成为未来的一股潮流。
在许多情况下,你需要根据条件做出决定。
为此,可以使用 if 语句。
例如:
x <- 24
if(x > 10){
print( "x is greater than 10")
}
你好,我是悦创。
R 语言是应用最广泛的统计编程语言。
而且 R 语言它是数据科学家和分析师的首选。
在本课程中,我们将学习R语言的基础知识,了解如何创建存储和操作数据的程序、以及如何使用各种数据集执行数据分析任务,以及如何使用图形和图表可视化结果。
本课程学习的技能可应用于任何与数据相关的领域,包括金融、数据科学、机器学习等。
Assignment 1 is designed to test your understanding of the skills practiced in weeks 1 through 4:
What kind of data structure is user_data
in the following declaration?
user_data = ("TJ", 24, "artLover123")
Netiquette is essentially rules and norms for interacting with others on the Internet.
We’re here to help with nine simple guidelines for how to be on your best behavior in an online classroom.
In order to participate in this course within Canvas, you should make sure that your technologies meet the minimal basic computer specificationsLinks to an external site..
Due Tuesday, October 10 at 11:55pm
This assignment gives you practice working with pointers, trees, and dynamic allocation. The program will be to implement the “Animal Guessing Game”. This game is a version of “20 Questions” where the only category is animals. You think of an animal and your program will try to guess the animal by asking you a series of “yes or no” questions.
(0 points) Please type your code and answers into Jupyter notebook. All visualizations should be prop-erly labelled. Submit the notebook as a pdf.
(3.5 points) Use the bwght dataset from the Wooldridge python module to answer the following question. You can find the documentation for the data online here. Import this data into your notebook.
(a) (1.5 Points) How many women are in the sample? What proportion of women with a family income higher than $50,000 are smokers? What proportion of women with a family income less than $20,000 are smokers?
(b) (1 Points) Generate a table of summary statistics for the dataframe. What is the average number of cigarettes smoked in a day? Is the mean a good measure of the typical women’s smoking habits? If no, explain why and if there is a better measure.
(c) (1 Points) Find the mode of fatheduc in the sample. Why are only 1,192 observations used to compute this statistic?
(5.5 points) Use the bwght dataset from the Wooldridge python module to answer the following question.
(a) (1 point) Generate two different histograms of bwght using Sturge’s and FD binning methods. Explain the strengths and weaknesses of each method when applied to bwght.
(b) (1 point) Create a histogram of bwght using either sturges or fd to choose the number of bins. Overlay a density curve.
(c) (2 points) Using a q-q plot, do you believe bwght is approximately normally distributed? Why are why not? What about family income?
(d) (1.5 points) Create a boxplot conditioning on whether or not the mother was a smoker. Do you observe any differences? If so, what are they?
(6 points) Use the bwght dataset from the Wooldridge python module to answer the following question.
(a) (2 points) Estimate the parameters for the following simple regression:
bwght^=β0^+β1^×packs
report the intercept and slope. What do these tell you about the association between cigarette use and birth weight?
(b) (2 points) What is the predicted value of birthweight when packs = 0? When packs = 2? What is the interpretation of the intercept?
(c) (1 point) Verify the residuals of this regression sum (approximately) to zero.
(d) (1 point) Using a scatter plot, show the observed values against the values predicted by a regression.
Create a list of your favorite 4 movies. Print out the 0th element in the list. Now set the 0th element to be “Star Wars” and try printing it out again.
创建你最喜欢的4部电影的列表。打印列表中的第0个元素。现在将第0个元素设置为“星球大战”,然后再试着打印它。
# 创建一个包含4部电影的列表
favorite_movies = ["Movie1", "Movie2", "Movie3", "Movie4"]
# 打印列表中的第0个元素
print(favorite_movies[0])
# 将第0个元素设置为“星球大战”
favorite_movies[0] = "星球大战"
# 再次打印列表中的第0个元素
print(favorite_movies[0])