You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

377 lines
20 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE HTML>
<html lang="zh-CN" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>函数 - Rust语言圣经(Rust Course)</title>
<!-- Custom HTML head -->
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../../favicon.svg">
<link rel="shortcut icon" href="../../favicon.png">
<link rel="stylesheet" href="../../css/variables.css">
<link rel="stylesheet" href="../../css/general.css">
<link rel="stylesheet" href="../../css/chrome.css">
<link rel="stylesheet" href="../../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" id="highlight-css" href="../../highlight.css">
<link rel="stylesheet" id="tomorrow-night-css" href="../../tomorrow-night.css">
<link rel="stylesheet" id="ayu-highlight-css" href="../../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<link rel="stylesheet" href="../../theme/style.css">
<!-- Provide site root and default themes to javascript -->
<script>
const path_to_root = "../../";
const default_light_theme = "light";
const default_dark_theme = "navy";
</script>
<!-- Start loading toc.js asap -->
<script src="../../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
let theme = localStorage.getItem('mdbook-theme');
let sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
let theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
let sidebar = null;
const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Rust语言圣经(Rust Course)</h1>
<div class="right-buttons">
<a href="../../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/sunface/rust-course" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/sunface/rust-course/edit/main/src/basic/base-type/function.md" title="Suggest an edit" aria-label="Suggest an edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="函数"><a class="header" href="#函数">函数</a></h1>
<p>Rust 的函数我们在之前已经见过不少,跟其他语言几乎没有什么区别。因此本章的学习之路将轻松和愉快,骚年们,请珍惜这种愉快,下一章你将体验到不一样的 Rust。</p>
<p>在函数界,有一个函数只闻其名不闻其声,可以止小孩啼!在程序界只有 <code>hello,world!</code> 可以与之媲美,它就是 <code>add</code> 函数:</p>
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>fn add(i: i32, j: i32) -&gt; i32 {
i + j
}
<span class="boring">}</span></code></pre></pre>
<p>该函数如此简单,但是又是如此的五脏俱全,声明函数的关键字 <code>fn</code>,函数名 <code>add()</code>,参数 <code>i</code><code>j</code>,参数类型和返回值类型都是 <code>i32</code>,总之一切那么的普通,但是又那么的自信,直到你看到了下面这张图:</p>
<img alt="" src="https://pic2.zhimg.com/80/v2-54b3a6d435d2482243edc4be9ab98153_1440w.png" class="center" />
<p>当你看懂了这张图,其实就等于差不多完成了函数章节的学习,但是这么短的章节显然对不起读者老爷们的厚爱,所以我们来展开下。</p>
<h2 id="函数要点"><a class="header" href="#函数要点">函数要点</a></h2>
<ul>
<li>函数名和变量名使用<a href="https://course.rs/practice/naming.html">蛇形命名法(snake case)</a>,例如 <code>fn add_two() {}</code></li>
<li>函数的位置可以随便放Rust 不关心我们在哪里定义了函数,只要有定义即可</li>
<li>每个函数参数都需要标注类型</li>
</ul>
<h2 id="函数参数"><a class="header" href="#函数参数">函数参数</a></h2>
<p>Rust 是静态类型语言,因此需要你为每一个函数参数都标识出它的具体类型,例如:</p>
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
another_function(5, 6.1);
}
fn another_function(x: i32, y: f32) {
println!("The value of x is: {}", x);
println!("The value of y is: {}", y);
}</code></pre></pre>
<p><code>another_function</code> 函数有两个参数,其中 <code>x</code><code>i32</code> 类型,<code>y</code><code>f32</code> 类型,然后在该函数内部,打印出这两个值。这里去掉 <code>x</code> 或者 <code>y</code> 的任何一个的类型,都会报错:</p>
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
another_function(5, 6.1);
}
fn another_function(x: i32, y) {
println!("The value of x is: {}", x);
println!("The value of y is: {}", y);
}</code></pre></pre>
<p>错误如下:</p>
<pre><code class="language-console">error: expected one of `:`, `@`, or `|`, found `)`
--&gt; src/main.rs:5:30
|
5 | fn another_function(x: i32, y) {
| ^ expected one of `:`, `@`, or `|` // 期待以下符号之一 `:`, `@`, or `|`
|
= note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
// 匿名参数在 Rust 2018 edition 中就已经移除
help: if this is a parameter name, give it a type // 如果y是一个参数名请给予它一个类型
|
5 | fn another_function(x: i32, y: TypeName) {
| ~~~~~~~~~~~
help: if this is a type, explicitly ignore the parameter name // 如果y是一个类型请使用_忽略参数名
|
5 | fn another_function(x: i32, _: y) {
| ~~~~
</code></pre>
<h2 id="函数返回"><a class="header" href="#函数返回">函数返回</a></h2>
<p>在上一章节语句和表达式中,我们有提到,在 Rust 中函数就是表达式,因此我们可以把函数的返回值直接赋给调用者。</p>
<p>函数的返回值就是函数体最后一条表达式的返回值,当然我们也可以使用 <code>return</code> 提前返回,下面的函数使用最后一条表达式来返回一个值:</p>
<pre><pre class="playground"><code class="language-rust edition2021">fn plus_five(x:i32) -&gt; i32 {
x + 5
}
fn main() {
let x = plus_five(5);
println!("The value of x is: {}", x);
}</code></pre></pre>
<p><code>x + 5</code> 是一条表达式,求值后,返回一个值,因为它是函数的最后一行,因此该表达式的值也是函数的返回值。</p>
<p>再来看两个重点:</p>
<ol>
<li><code>let x = plus_five(5)</code>,说明我们用一个函数的返回值来初始化 <code>x</code> 变量,因此侧面说明了在 Rust 中函数也是表达式,这种写法等同于 <code>let x = 5 + 5;</code></li>
<li><code>x + 5</code> 没有分号,因为它是一条表达式,这个在上一节中我们也有详细介绍</li>
</ol>
<p>再来看一段代码,同时使用 <code>return</code> 和表达式作为返回值:</p>
<pre><pre class="playground"><code class="language-rust edition2021">fn plus_or_minus(x:i32) -&gt; i32 {
if x &gt; 5 {
return x - 5
}
x + 5
}
fn main() {
let x = plus_or_minus(5);
println!("The value of x is: {}", x);
}</code></pre></pre>
<p><code>plus_or_minus</code> 函数根据传入 <code>x</code> 的大小来决定是做加法还是减法,若 <code>x &gt; 5</code> 则通过 <code>return</code> 提前返回 <code>x - 5</code> 的值,否则返回 <code>x + 5</code> 的值。</p>
<h4 id="rust-中的特殊返回类型"><a class="header" href="#rust-中的特殊返回类型">Rust 中的特殊返回类型</a></h4>
<h5 id="无返回值"><a class="header" href="#无返回值">无返回值<code>()</code></a></h5>
<p>对于 Rust 新手来说,有些返回类型很难理解,而且如果你想通过百度或者谷歌去搜索,都不好查询,因为这些符号太常见了,根本难以精确搜索到。</p>
<p>例如单元类型 <code>()</code>,是一个零长度的元组。它没啥作用,但是可以用来表达一个函数没有返回值:</p>
<ul>
<li>函数没有返回值,那么返回一个 <code>()</code></li>
<li>通过 <code>;</code> 结尾的语句返回一个 <code>()</code></li>
</ul>
<p>例如下面的 <code>report</code> 函数会隐式返回一个 <code>()</code></p>
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>use std::fmt::Debug;
fn report&lt;T: Debug&gt;(item: T) {
println!("{:?}", item);
}
<span class="boring">}</span></code></pre></pre>
<p>与上面的函数返回值相同,但是下面的函数显式的返回了 <code>()</code></p>
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>fn clear(text: &amp;mut String) -&gt; () {
*text = String::from("");
}
<span class="boring">}</span></code></pre></pre>
<p>在实际编程中,你会经常在错误提示中看到该 <code>()</code> 的身影出没,假如你的函数需要返回一个 <code>u32</code> 值,但是如果你不幸的以 <code>表达式;</code> 的语句形式作为函数的最后一行代码,就会报错:</p>
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>fn add(x:u32,y:u32) -&gt; u32 {
x + y;
}
<span class="boring">}</span></code></pre></pre>
<p>错误如下:</p>
<pre><code class="language-console">error[E0308]: mismatched types // 类型不匹配
--&gt; src/main.rs:6:24
|
6 | fn add(x:u32,y:u32) -&gt; u32 {
| --- ^^^ expected `u32`, found `()` // 期望返回u32,却返回()
| |
| implicitly returns `()` as its body has no tail or `return` expression
7 | x + y;
| - help: consider removing this semicolon
</code></pre>
<p>还记得我们在<a href="https://course.rs/basic/base-type/statement-expression.html">语句与表达式</a>中讲过的吗?只有表达式能返回值,而 <code>;</code> 结尾的是语句,在 Rust 中,一定要严格区分<strong>表达式</strong><strong>语句</strong>的区别,这个在其它语言中往往是被忽视的点。</p>
<h5 id="永不返回的发散函数-"><a class="header" href="#永不返回的发散函数-">永不返回的发散函数 <code>!</code></a></h5>
<p>当用 <code>!</code> 作函数返回类型的时候,表示该函数永不返回( diverging functions ),特别的,这种语法往往用做会导致程序崩溃的函数:</p>
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>fn dead_end() -&gt; ! {
panic!("你已经到了穷途末路,崩溃吧!");
}
<span class="boring">}</span></code></pre></pre>
<p>下面的函数创建了一个无限循环,该循环永不跳出,因此函数也永不返回:</p>
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>fn forever() -&gt; ! {
loop {
//...
};
}
<span class="boring">}</span></code></pre></pre>
<h2 id="课后练习"><a class="header" href="#课后练习">课后练习</a></h2>
<blockquote>
<p><a href="https://practice-zh.course.rs/basic-types/functions.html">Rust By Practice</a>,支持代码在线编辑和运行,并提供详细的<a href="https://github.com/sunface/rust-by-practice/blob/master/solutions/basic-types/functions.md">习题解答</a></p>
</blockquote>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../../basic/base-type/statement-expression.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../../basic/ownership/index.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../../basic/base-type/statement-expression.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../../basic/ownership/index.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="../../ace.js"></script>
<script src="../../editor.js"></script>
<script src="../../mode-rust.js"></script>
<script src="../../theme-dawn.js"></script>
<script src="../../theme-tomorrow_night.js"></script>
<script src="../../elasticlunr.min.js"></script>
<script src="../../mark.min.js"></script>
<script src="../../searcher.js"></script>
<script src="../../clipboard.min.js"></script>
<script src="../../highlight.js"></script>
<script src="../../book.js"></script>
<!-- Custom JS scripts -->
<script src="../../assets/custom2.js"></script>
<script src="../../assets/bigPicture.js"></script>
</div>
</body>
</html>