|
|
|
|
<!DOCTYPE HTML>
|
|
|
|
|
<html lang="zh-CN" class="sidebar-visible no-js light">
|
|
|
|
|
<head>
|
|
|
|
|
<!-- Book generated using mdBook -->
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<title>所有权 - Rust语言圣经(Rust Course)</title>
|
|
|
|
|
<!-- Custom HTML head -->
|
|
|
|
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
|
|
|
<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" href="../../highlight.css">
|
|
|
|
|
<link rel="stylesheet" href="../../tomorrow-night.css">
|
|
|
|
|
<link rel="stylesheet" href="../../ayu-highlight.css">
|
|
|
|
|
|
|
|
|
|
<!-- Custom theme stylesheets -->
|
|
|
|
|
<link rel="stylesheet" href="../../theme/style.css">
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<!-- Provide site root to javascript -->
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
var path_to_root = "../../";
|
|
|
|
|
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
try {
|
|
|
|
|
var theme = localStorage.getItem('mdbook-theme');
|
|
|
|
|
var 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 type="text/javascript">
|
|
|
|
|
var theme;
|
|
|
|
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
|
|
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
|
|
|
var html = document.querySelector('html');
|
|
|
|
|
html.classList.remove('no-js')
|
|
|
|
|
html.classList.remove('light')
|
|
|
|
|
html.classList.add(theme);
|
|
|
|
|
html.classList.add('js');
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<!-- Hide / unhide sidebar before it is displayed -->
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
var html = document.querySelector('html');
|
|
|
|
|
var sidebar = 'hidden';
|
|
|
|
|
if (document.body.clientWidth >= 1080) {
|
|
|
|
|
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
|
|
|
sidebar = sidebar || 'visible';
|
|
|
|
|
}
|
|
|
|
|
html.classList.remove('sidebar-visible');
|
|
|
|
|
html.classList.add("sidebar-" + sidebar);
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
|
|
|
<div class="sidebar-scrollbox">
|
|
|
|
|
<ol class="chapter"><li class="chapter-item affix "><a href="../../about-book.html">关于本书</a></li><li class="chapter-item affix "><a href="../../into-rust.html">进入 Rust 编程世界</a></li><li class="chapter-item affix "><a href="../../first-try/sth-you-should-not-do.html">避免从入门到放弃</a></li><li class="chapter-item affix "><a href="../../community.html">社区和锈书</a></li><li class="chapter-item affix "><li class="part-title">Rust 语言基础学习</li><li class="spacer"></li><li class="chapter-item "><a href="../../first-try/intro.html"><strong aria-hidden="true">1.</strong> 寻找牛刀,以便小试</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../first-try/installation.html"><strong aria-hidden="true">1.1.</strong> 安装 Rust 环境</a></li><li class="chapter-item "><a href="../../first-try/editor.html"><strong aria-hidden="true">1.2.</strong> 墙推 VSCode!</a></li><li class="chapter-item "><a href="../../first-try/cargo.html"><strong aria-hidden="true">1.3.</strong> 认识 Cargo</a></li><li class="chapter-item "><a href="../../first-try/hello-world.html"><strong aria-hidden="true">1.4.</strong> 不仅仅是 Hello world</a></li><li class="chapter-item "><a href="../../first-try/slowly-downloading.html"><strong aria-hidden="true">1.5.</strong> 下载依赖太慢了?</a></li></ol></li><li class="chapter-item expanded "><a href="../../basic/intro.html"><strong aria-hidden="true">2.</strong> Rust 基础入门</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/variable.html"><strong aria-hidden="true">2.1.</strong> 变量绑定与解构</a></li><li class="chapter-item "><a href="../../basic/base-type/index.html"><strong aria-hidden="true">2.2.</strong> 基本类型</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/base-type/numbers.html"><strong aria-hidden="true">2.2.1.</strong> 数值类型</a></li><li class="chapter-item "><a href="../../basic/base-type/char-bool.html"><strong aria-hidden="true">2.2.2.</strong> 字符、布尔、单元类型</a></li><li class="chapter-item "><a href="../../basic/base-type/statement-expression.html"><strong aria-hidden="true">2.2.3.</strong> 语句与表达式</a></li><li class="chapter-item "><a href="../../basic/base-type/function.html"><strong aria-hidden="true">2.2.4.</strong> 函数</a></li></ol></li><li class="chapter-item expanded "><a href="../../basic/ownership/index.html"><strong aria-hidden="true">2.3.</strong> 所有权和借用</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../basic/ownership/ownership.html" class="active"><strong aria-hidden="true">2.3.1.</strong> 所有权</a></li><li class="chapter-item "><a href="../../basic/ownership/borrowing.html"><strong aria-hidden="true">2.3.2.</strong> 引用与借用</a></li></ol></li><li class="chapter-item "><a href="../../basic/compound-type/intro.html"><strong aria-hidden="true">2.4.</strong> 复合类型</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/compound-type/string-slice.html"><strong aria-hidden="true">2.4.1.</strong> 字符串与切片</a></li><li class="chapter-item "><a href="../../basic/compound-type/tuple.html"><strong aria-hidden="true">2.4.2.</strong> 元组</a></li><li class="chapter-item "><a href="../../basic/compound-type/struct.html"><strong aria-hidden="true">2.4.3.</strong> 结构体</a></li><li class="chapter-item "><a href="../../basic/compound-type/enum.html"><strong aria-hidden="true">2.4.4.</strong> 枚举</a></li><li class="chapter-item "><a href="../../basic/compound-type/array.html"><strong aria-hidden="true">2.4.5.</strong> 数组</a></li></ol></li><li class="chapter-item "><a href="../../basic/flow-control.html"><strong aria-hidden="true">2.5.</strong> 流程控制</a></li><li class="chapter-item "><a href="../../basic/ma
|
|
|
|
|
</div>
|
|
|
|
|
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></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 bordered">
|
|
|
|
|
<div class="left-buttons">
|
|
|
|
|
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
|
|
|
<i class="fa fa-bars"></i>
|
|
|
|
|
</button>
|
|
|
|
|
<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="light">Light (default)</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/ownership/ownership.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 type="text/javascript">
|
|
|
|
|
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
|
|
|
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
|
|
|
|
|
|
|
|
// Get viewed page store
|
|
|
|
|
var viewed_key = 'mdbook-viewed';
|
|
|
|
|
var viewed_map = {};
|
|
|
|
|
try {
|
|
|
|
|
var viewed_storage = localStorage.getItem(viewed_key);
|
|
|
|
|
if (viewed_storage) {
|
|
|
|
|
viewed_map = JSON.parse(viewed_storage)
|
|
|
|
|
}
|
|
|
|
|
} catch (e) { }
|
|
|
|
|
|
|
|
|
|
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
|
|
|
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
|
|
|
|
|
|
|
|
// Apply viewed style
|
|
|
|
|
if (viewed_map[link.pathname]) {
|
|
|
|
|
link.classList.add('md-viewed')
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Mark viewed after 30s
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
viewed_map[location.pathname] = 1;
|
|
|
|
|
localStorage.setItem(viewed_key, JSON.stringify(viewed_map));
|
|
|
|
|
}, 30000)
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<div id="content" class="content">
|
|
|
|
|
<!-- Page table of contents -->
|
|
|
|
|
<div class="sidetoc"><nav class="pagetoc"></nav></div>
|
|
|
|
|
<main>
|
|
|
|
|
<h1 id="所有权"><a class="header" href="#所有权">所有权</a></h1>
|
|
|
|
|
<p>所有的程序都必须和计算机内存打交道,如何从内存中申请空间来存放程序的运行内容,如何在不需要的时候释放这些空间,成了重中之重,也是所有编程语言设计的难点之一。在计算机语言不断演变过程中,出现了三种流派:</p>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><strong>垃圾回收机制(GC)</strong>,在程序运行时不断寻找不再使用的内存,典型代表:Java、Go</li>
|
|
|
|
|
<li><strong>手动管理内存的分配和释放</strong>, 在程序中,通过函数调用的方式来申请和释放内存,典型代表:C++</li>
|
|
|
|
|
<li><strong>通过所有权来管理内存</strong>,编译器在编译时会根据一系列规则进行检查</li>
|
|
|
|
|
</ul>
|
|
|
|
|
<p>其中 Rust 选择了第三种,最妙的是,这种检查只发生在编译期,因此对于程序运行期,不会有任何性能上的损失。</p>
|
|
|
|
|
<p>由于所有权是一个新概念,因此读者需要花费一些时间来掌握它,一旦掌握,海阔天空任你飞跃,在本章,我们将通过 <code>字符串</code> 来引导讲解所有权的相关知识。</p>
|
|
|
|
|
<h2 id="一段不安全的代码"><a class="header" href="#一段不安全的代码">一段不安全的代码</a></h2>
|
|
|
|
|
<p>先来看看一段来自 C 语言的糟糕代码:</p>
|
|
|
|
|
<pre><code class="language-c">int* foo() {
|
|
|
|
|
int a; // 变量a的作用域开始
|
|
|
|
|
a = 100;
|
|
|
|
|
char *c = "xyz"; // 变量c的作用域开始
|
|
|
|
|
return &a;
|
|
|
|
|
} // 变量a和c的作用域结束
|
|
|
|
|
</code></pre>
|
|
|
|
|
<p>这段代码虽然可以编译通过,但是其实非常糟糕,变量 <code>a</code> 和 <code>c</code> 都是局部变量,函数结束后将局部变量 <code>a</code> 的地址返回,但局部变量 <code>a</code> 存在栈中,在离开作用域后,<code>a</code> 所申请的栈上内存都会被系统回收,从而造成了 <code>悬空指针(Dangling Pointer)</code> 的问题。这是一个非常典型的内存安全问题,虽然编译可以通过,但是运行的时候会出现错误, 很多编程语言都存在。</p>
|
|
|
|
|
<p>再来看变量 <code>c</code>,<code>c</code> 的值是常量字符串,存储于常量区,可能这个函数我们只调用了一次,也可能我们不再会使用这个字符串,但 <code>"xyz"</code> 只有当整个程序结束后系统才能回收这片内存。</p>
|
|
|
|
|
<p>所以内存安全问题,一直都是程序员非常头疼的问题,好在, 在 Rust 中这些问题即将成为历史,因为 Rust 在编译的时候就可以帮助我们发现内存不安全的问题,那 Rust 如何做到这一点呢?</p>
|
|
|
|
|
<p>在正式进入主题前,先来一个预热知识。</p>
|
|
|
|
|
<h2 id="栈stack与堆heap"><a class="header" href="#栈stack与堆heap">栈(Stack)与堆(Heap)</a></h2>
|
|
|
|
|
<p>栈和堆是编程语言最核心的数据结构,但是在很多语言中,你并不需要深入了解栈与堆。 但对于 Rust 这样的系统编程语言,值是位于栈上还是堆上非常重要, 因为这会影响程序的行为和性能。</p>
|
|
|
|
|
<p>栈和堆的核心目标就是为程序在运行时提供可供使用的内存空间。</p>
|
|
|
|
|
<h4 id="栈"><a class="header" href="#栈">栈</a></h4>
|
|
|
|
|
<p>栈按照顺序存储值并以相反顺序取出值,这也被称作<strong>后进先出</strong>。想象一下一叠盘子:当增加更多盘子时,把它们放在盘子堆的顶部,当需要盘子时,再从顶部拿走。不能从中间也不能从底部增加或拿走盘子!</p>
|
|
|
|
|
<p>增加数据叫做<strong>进栈</strong>,移出数据则叫做<strong>出栈</strong>。</p>
|
|
|
|
|
<p>因为上述的实现方式,栈中的所有数据都必须占用已知且固定大小的内存空间,假设数据大小是未知的,那么在取出数据时,你将无法取到你想要的数据。</p>
|
|
|
|
|
<h4 id="堆"><a class="header" href="#堆">堆</a></h4>
|
|
|
|
|
<p>与栈不同,对于大小未知或者可能变化的数据,我们需要将它存储在堆上。</p>
|
|
|
|
|
<p>当向堆上放入数据时,需要请求一定大小的内存空间。操作系统在堆的某处找到一块足够大的空位,把它标记为已使用,并返回一个表示该位置地址的<strong>指针</strong>, 该过程被称为<strong>在堆上分配内存</strong>,有时简称为 “分配”(allocating)。</p>
|
|
|
|
|
<p>接着,该指针会被推入<strong>栈</strong>中,因为指针的大小是已知且固定的,在后续使用过程中,你将通过栈中的<strong>指针</strong>,来获取数据在堆上的实际内存位置,进而访问该数据。</p>
|
|
|
|
|
<p>由上可知,堆是一种缺乏组织的数据结构。想象一下去餐馆就座吃饭: 进入餐馆,告知服务员有几个人,然后服务员找到一个够大的空桌子(堆上分配的内存空间)并领你们过去。如果有人来迟了,他们也可以通过桌号(栈上的指针)来找到你们坐在哪。</p>
|
|
|
|
|
<h4 id="性能区别"><a class="header" href="#性能区别">性能区别</a></h4>
|
|
|
|
|
<p>写入方面:入栈比在堆上分配内存要快,因为入栈时操作系统无需分配新的空间,只需要将新数据放入栈顶即可。相比之下,在堆上分配内存则需要更多的工作,这是因为操作系统必须首先找到一块足够存放数据的内存空间,接着做一些记录为下一次分配做准备。</p>
|
|
|
|
|
<p>读取方面:得益于 CPU 高速缓存,使得处理器可以减少对内存的访问,高速缓存和内存的访问速度差异在 10 倍以上!栈数据往往可以直接存储在 CPU 高速缓存中,而堆数据只能存储在内存中。访问堆上的数据比访问栈上的数据慢,因为必须先访问栈再通过栈上的指针来访问内存。</p>
|
|
|
|
|
<p>因此,处理器处理分配在栈上数据会比在堆上的数据更加高效。</p>
|
|
|
|
|
<h4 id="所有权与堆栈"><a class="header" href="#所有权与堆栈">所有权与堆栈</a></h4>
|
|
|
|
|
<p>当你的代码调用一个函数时,传递给函数的参数(包括可能指向堆上数据的指针和函数的局部变量)依次被压入栈中,当函数调用结束时,这些值将被从栈中按照相反的顺序依次移除。</p>
|
|
|
|
|
<p>因为堆上的数据缺乏组织,因此跟踪这些数据何时分配和释放是非常重要的,否则堆上的数据将产生内存泄漏 —— 这些数据将永远无法被回收。这就是 Rust 所有权系统为我们提供的强大保障。</p>
|
|
|
|
|
<p>对于其他很多编程语言,你确实无需理解堆栈的原理,但是<strong>在 Rust 中,明白堆栈的原理,对于我们理解所有权的工作原理会有很大的帮助</strong>。</p>
|
|
|
|
|
<h2 id="所有权原则"><a class="header" href="#所有权原则">所有权原则</a></h2>
|
|
|
|
|
<p>理解了堆栈,接下来看一下<em>关于所有权的规则</em>,首先请谨记以下规则:</p>
|
|
|
|
|
<blockquote>
|
|
|
|
|
<ol>
|
|
|
|
|
<li>Rust 中每一个值都被一个变量所拥有,该变量被称为值的所有者</li>
|
|
|
|
|
<li>一个值同时只能被一个变量所拥有,或者说一个值只能拥有一个所有者</li>
|
|
|
|
|
<li>当所有者(变量)离开作用域范围时,这个值将被丢弃(drop)</li>
|
|
|
|
|
</ol>
|
|
|
|
|
</blockquote>
|
|
|
|
|
<h4 id="变量作用域"><a class="header" href="#变量作用域">变量作用域</a></h4>
|
|
|
|
|
<p>作用域是一个变量在程序中有效的范围, 假如有这样一个变量:</p>
|
|
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">
|
|
|
|
|
<span class="boring">#![allow(unused)]
|
|
|
|
|
</span><span class="boring">fn main() {
|
|
|
|
|
</span>let s = "hello";
|
|
|
|
|
<span class="boring">}
|
|
|
|
|
</span></code></pre></pre>
|
|
|
|
|
<p>变量 <code>s</code> 绑定到了一个字符串字面值,该字符串字面值是硬编码到程序代码中的。<code>s</code> 变量从声明的点开始直到当前作用域的结束都是有效的:</p>
|
|
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">
|
|
|
|
|
<span class="boring">#![allow(unused)]
|
|
|
|
|
</span><span class="boring">fn main() {
|
|
|
|
|
</span>{ // s 在这里无效,它尚未声明
|
|
|
|
|
let s = "hello"; // 从此处起,s 是有效的
|
|
|
|
|
|
|
|
|
|
// 使用 s
|
|
|
|
|
} // 此作用域已结束,s不再有效
|
|
|
|
|
<span class="boring">}
|
|
|
|
|
</span></code></pre></pre>
|
|
|
|
|
<p>简而言之,<code>s</code> 从创建伊始就开始有效,然后有效期持续到它离开作用域为止,可以看出,就作用域来说,Rust 语言跟其他编程语言没有区别。</p>
|
|
|
|
|
<h4 id="简单介绍-string-类型"><a class="header" href="#简单介绍-string-类型">简单介绍 String 类型</a></h4>
|
|
|
|
|
<p>之前提到过,本章会用 <code>String</code> 作为例子,因此这里会进行一下简单的介绍,具体的 <code>String</code> 学习请参见 <a href="https://course.rs/basic/compound-type/string-slice.html">String 类型</a>。</p>
|
|
|
|
|
<p>我们已经见过字符串字面值 <code>let s ="hello"</code>,<code>s</code> 是被硬编码进程序里的字符串值(类型为 <code>&str</code> )。字符串字面值是很方便的,但是它并不适用于所有场景。原因有二:</p>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><strong>字符串字面值是不可变的</strong>,因为被硬编码到程序代码中</li>
|
|
|
|
|
<li>并非所有字符串的值都能在编写代码时得知</li>
|
|
|
|
|
</ul>
|
|
|
|
|
<p>例如,字符串是需要程序运行时,通过用户动态输入然后存储在内存中的,这种情况,字符串字面值就完全无用武之地。 为此,Rust 为我们提供动态字符串类型: <code>String</code>, 该类型被分配到堆上,因此可以动态伸缩,也就能存储在编译时大小未知的文本。</p>
|
|
|
|
|
<p>可以使用下面的方法基于字符串字面量来创建 <code>String</code> 类型:</p>
|
|
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">
|
|
|
|
|
<span class="boring">#![allow(unused)]
|
|
|
|
|
</span><span class="boring">fn main() {
|
|
|
|
|
</span>let s = String::from("hello");
|
|
|
|
|
<span class="boring">}
|
|
|
|
|
</span></code></pre></pre>
|
|
|
|
|
<p><code>::</code> 是一种调用操作符,这里表示调用 <code>String</code> 中的 <code>from</code> 方法,因为 <code>String</code> 存储在堆上是动态的,你可以这样修改它:</p>
|
|
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">
|
|
|
|
|
<span class="boring">#![allow(unused)]
|
|
|
|
|
</span><span class="boring">fn main() {
|
|
|
|
|
</span>let mut s = String::from("hello");
|
|
|
|
|
|
|
|
|
|
s.push_str(", world!"); // push_str() 在字符串后追加字面值
|
|
|
|
|
|
|
|
|
|
println!("{}", s); // 将打印 `hello, world!`
|
|
|
|
|
<span class="boring">}
|
|
|
|
|
</span></code></pre></pre>
|
|
|
|
|
<p>言归正传,了解 <code>String</code> 内容后,一起来看看关于所有权的交互。</p>
|
|
|
|
|
<h2 id="变量绑定背后的数据交互"><a class="header" href="#变量绑定背后的数据交互">变量绑定背后的数据交互</a></h2>
|
|
|
|
|
<h4 id="转移所有权"><a class="header" href="#转移所有权">转移所有权</a></h4>
|
|
|
|
|
<p>先来看一段代码:</p>
|
|
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">
|
|
|
|
|
<span class="boring">#![allow(unused)]
|
|
|
|
|
</span><span class="boring">fn main() {
|
|
|
|
|
</span>let x = 5;
|
|
|
|
|
let y = x;
|
|
|
|
|
<span class="boring">}
|
|
|
|
|
</span></code></pre></pre>
|
|
|
|
|
<p>代码背后的逻辑很简单, 将 <code>5</code> 绑定到变量 <code>x</code>;接着拷贝 <code>x</code> 的值赋给 <code>y</code>,最终 <code>x</code> 和 <code>y</code> 都等于 <code>5</code>,因为整数是 Rust 基本数据类型,是固定大小的简单值,因此这两个值都是通过自动拷贝的方式来赋值的,都被存在栈中,完全无需在堆上分配内存。</p>
|
|
|
|
|
<p>可能有同学会有疑问:这种拷贝不消耗性能吗?实际上,这种栈上的数据足够简单,而且拷贝非常非常快,只需要复制一个整数大小(<code>i32</code>,4 个字节)的内存即可,因此在这种情况下,拷贝的速度远比在堆上创建内存来得快的多。实际上,上一章我们讲到的 Rust 基本类型都是通过自动拷贝的方式来赋值的,就像上面代码一样。</p>
|
|
|
|
|
<p>然后再来看一段代码:</p>
|
|
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">
|
|
|
|
|
<span class="boring">#![allow(unused)]
|
|
|
|
|
</span><span class="boring">fn main() {
|
|
|
|
|
</span>let s1 = String::from("hello");
|
|
|
|
|
let s2 = s1;
|
|
|
|
|
<span class="boring">}
|
|
|
|
|
</span></code></pre></pre>
|
|
|
|
|
<p>此时,可能某个大聪明(善意昵称)已经想到了:嗯,把 <code>s1</code> 的内容拷贝一份赋值给 <code>s2</code>,实际上,并不是这样。之前也提到了,对于基本类型(存储在栈上),Rust 会自动拷贝,但是 <code>String</code> 不是基本类型,而且是存储在堆上的,因此不能自动拷贝。</p>
|
|
|
|
|
<p>实际上, <code>String</code> 类型是一个复杂类型,由<strong>存储在栈中的堆指针</strong>、<strong>字符串长度</strong>、<strong>字符串容量</strong>共同组成,其中<strong>堆指针</strong>是最重要的,它指向了真实存储字符串内容的堆内存,至于长度和容量,如果你有 Go 语言的经验,这里就很好理解:容量是堆内存分配空间的大小,长度是目前已经使用的大小。</p>
|
|
|
|
|
<p>总之 <code>String</code> 类型指向了一个堆上的空间,这里存储着它的真实数据, 下面对上面代码中的 <code>let s2 = s1</code> 分成两种情况讨论:</p>
|
|
|
|
|
<ol>
|
|
|
|
|
<li>
|
|
|
|
|
<p>拷贝 <code>String</code> 和存储在堆上的字节数组
|
|
|
|
|
如果该语句是拷贝所有数据(深拷贝),那么无论是 <code>String</code> 本身还是底层的堆上数据,都会被全部拷贝,这对于性能而言会造成非常大的影响</p>
|
|
|
|
|
</li>
|
|
|
|
|
<li>
|
|
|
|
|
<p>只拷贝 <code>String</code> 本身
|
|
|
|
|
这样的拷贝非常快,因为在 64 位机器上就拷贝了 <code>8字节的指针</code>、<code>8字节的长度</code>、<code>8字节的容量</code>,总计 24 字节,但是带来了新的问题,还记得我们之前提到的所有权规则吧?其中有一条就是:<strong>一个值只允许有一个所有者</strong>,而现在这个值(堆上的真实字符串数据)有了两个所有者:<code>s1</code> 和 <code>s2</code>。</p>
|
|
|
|
|
</li>
|
|
|
|
|
</ol>
|
|
|
|
|
<p>好吧,就假定一个值可以拥有两个所有者,会发生什么呢?</p>
|
|
|
|
|
<p>当变量离开作用域后,Rust 会自动调用 <code>drop</code> 函数并清理变量的堆内存。不过由于两个 <code>String</code> 变量指向了同一位置。这就有了一个问题:当 <code>s1</code> 和 <code>s2</code> 离开作用域,它们都会尝试释放相同的内存。这是一个叫做 <strong>二次释放(double free)</strong> 的错误,也是之前提到过的内存安全性 BUG 之一。两次释放(相同)内存会导致内存污染,它可能会导致潜在的安全漏洞。</p>
|
|
|
|
|
<p>因此,Rust 这样解决问题:<strong>当 <code>s1</code> 赋予 <code>s2</code> 后,Rust 认为 <code>s1</code> 不再有效,因此也无需在 <code>s1</code> 离开作用域后 <code>drop</code> 任何东西,这就是把所有权从 <code>s1</code> 转移给了 <code>s2</code>,<code>s1</code> 在被赋予 <code>s2</code> 后就马上失效了</strong>。</p>
|
|
|
|
|
<p>再来看看,在所有权转移后再来使用旧的所有者,会发生什么:</p>
|
|
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">
|
|
|
|
|
<span class="boring">#![allow(unused)]
|
|
|
|
|
</span><span class="boring">fn main() {
|
|
|
|
|
</span>let s1 = String::from("hello");
|
|
|
|
|
let s2 = s1;
|
|
|
|
|
|
|
|
|
|
println!("{}, world!", s1);
|
|
|
|
|
<span class="boring">}
|
|
|
|
|
</span></code></pre></pre>
|
|
|
|
|
<p>由于 Rust 禁止你使用无效的引用,你会看到以下的错误:</p>
|
|
|
|
|
<pre><code class="language-console">error[E0382]: use of moved value: `s1`
|
|
|
|
|
--> src/main.rs:5:28
|
|
|
|
|
|
|
|
|
|
|
3 | let s2 = s1;
|
|
|
|
|
| -- value moved here
|
|
|
|
|
4 |
|
|
|
|
|
5 | println!("{}, world!", s1);
|
|
|
|
|
| ^^ value used here after move
|
|
|
|
|
|
|
|
|
|
|
= note: move occurs because `s1` has type `std::string::String`, which does
|
|
|
|
|
not implement the `Copy` trait
|
|
|
|
|
</code></pre>
|
|
|
|
|
<p>现在再回头看看之前的规则,相信大家已经有了更深刻的理解:</p>
|
|
|
|
|
<blockquote>
|
|
|
|
|
<ol>
|
|
|
|
|
<li>Rust 中每一个值都被一个变量所拥有,该变量被称为值的所有者</li>
|
|
|
|
|
<li>一个值同时只能被一个变量所拥有,或者说一个值只能拥有一个所有者</li>
|
|
|
|
|
<li>当所有者(变量)离开作用域范围时,这个值将被丢弃(drop)</li>
|
|
|
|
|
</ol>
|
|
|
|
|
</blockquote>
|
|
|
|
|
<p>如果你在其他语言中听说过术语 <strong>浅拷贝(shallow copy)</strong> 和 <strong>深拷贝(deep copy)</strong>,那么拷贝指针、长度和容量而不拷贝数据听起来就像浅拷贝,但是又因为 Rust 同时使第一个变量 <code>s1</code> 无效了,因此这个操作被称为 <strong>移动(move)</strong>,而不是浅拷贝。上面的例子可以解读为 <code>s1</code> 被<strong>移动</strong>到了 <code>s2</code> 中。那么具体发生了什么,用一张图简单说明:</p>
|
|
|
|
|
<img alt="s1 moved to s2" src="https://pic1.zhimg.com/80/v2-3ec77951de6a17584b5eb4a3838b4b61_1440w.jpg" class="center" style="width: 50%;" />
|
|
|
|
|
<p>这样就解决了我们之前的问题,<code>s1</code> 不再指向任何数据,只有 <code>s2</code> 是有效的,当 <code>s2</code> 离开作用域,它就会释放内存。 相信此刻,你应该明白了,为什么 Rust 称呼 <code>let a = b</code> 为<strong>变量绑定</strong>了吧?</p>
|
|
|
|
|
<p>再来看一段代码:</p>
|
|
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
|
|
|
|
|
let x: &str = "hello, world";
|
|
|
|
|
let y = x;
|
|
|
|
|
println!("{},{}",x,y);
|
|
|
|
|
}
|
|
|
|
|
</code></pre></pre>
|
|
|
|
|
<p>这段代码,大家觉得会否报错?如果参考之前的 <code>String</code> 所有权转移的例子,那这段代码也应该报错才是,但是实际上呢?</p>
|
|
|
|
|
<p>这段代码和之前的 <code>String</code> 有一个本质上的区别:在 <code>String</code> 的例子中 <code>s1</code> 持有了通过<code>String::from("hello")</code> 创建的值的所有权,而这个例子中,<code>x</code> 只是引用了存储在二进制中的字符串 <code>"hello, world"</code>,并没有持有所有权。</p>
|
|
|
|
|
<p>因此 <code>let y = x</code> 中,仅仅是对该引用进行了拷贝,此时 <code>y</code> 和 <code>x</code> 都引用了同一个字符串。<strong>如果还不理解也没关系,当学习了下一章节 "引用与借用" 后,大家自然而言就会理解。</strong></p>
|
|
|
|
|
<h4 id="克隆深拷贝"><a class="header" href="#克隆深拷贝">克隆(深拷贝)</a></h4>
|
|
|
|
|
<p>首先,<strong>Rust 永远也不会自动创建数据的 “深拷贝”</strong>。因此,任何<strong>自动</strong>的复制都不是深拷贝,可以被认为对运行时性能影响较小。</p>
|
|
|
|
|
<p>如果我们<strong>确实</strong>需要深度复制 <code>String</code> 中堆上的数据,而不仅仅是栈上的数据,可以使用一个叫做 <code>clone</code> 的方法。</p>
|
|
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">
|
|
|
|
|
<span class="boring">#![allow(unused)]
|
|
|
|
|
</span><span class="boring">fn main() {
|
|
|
|
|
</span>let s1 = String::from("hello");
|
|
|
|
|
let s2 = s1.clone();
|
|
|
|
|
|
|
|
|
|
println!("s1 = {}, s2 = {}", s1, s2);
|
|
|
|
|
<span class="boring">}
|
|
|
|
|
</span></code></pre></pre>
|
|
|
|
|
<p>这段代码能够正常运行,因此说明 <code>s2</code> 确实完整的复制了 <code>s1</code> 的数据。</p>
|
|
|
|
|
<p>如果代码性能无关紧要,例如初始化程序时,或者在某段时间只会执行一次时,你可以使用 <code>clone</code> 来简化编程。但是对于执行较为频繁的代码(热点路径),使用 <code>clone</code> 会极大的降低程序性能,需要小心使用!</p>
|
|
|
|
|
<h4 id="拷贝浅拷贝"><a class="header" href="#拷贝浅拷贝">拷贝(浅拷贝)</a></h4>
|
|
|
|
|
<p>浅拷贝只发生在栈上,因此性能很高,在日常编程中,浅拷贝无处不在。</p>
|
|
|
|
|
<p>再回到之前看过的例子:</p>
|
|
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">
|
|
|
|
|
<span class="boring">#![allow(unused)]
|
|
|
|
|
</span><span class="boring">fn main() {
|
|
|
|
|
</span>let x = 5;
|
|
|
|
|
let y = x;
|
|
|
|
|
|
|
|
|
|
println!("x = {}, y = {}", x, y);
|
|
|
|
|
<span class="boring">}
|
|
|
|
|
</span></code></pre></pre>
|
|
|
|
|
<p>但这段代码似乎与我们刚刚学到的内容相矛盾:没有调用 <code>clone</code>,不过依然实现了类似深拷贝的效果 —— 没有报所有权的错误。</p>
|
|
|
|
|
<p>原因是像整型这样的基本类型在编译时是已知大小的,会被存储在栈上,所以拷贝其实际的值是快速的。这意味着没有理由在创建变量 <code>y</code> 后使 <code>x</code> 无效(<code>x</code>、<code>y</code> 都仍然有效)。换句话说,这里没有深浅拷贝的区别,因此这里调用 <code>clone</code> 并不会与通常的浅拷贝有什么不同,我们可以不用管它(可以理解成在栈上做了深拷贝)。</p>
|
|
|
|
|
<p>Rust 有一个叫做 <code>Copy</code> 的特征,可以用在类似整型这样在栈中存储的类型。如果一个类型拥有 <code>Copy</code> 特征,一个旧的变量在被赋值给其他变量后仍然可用。</p>
|
|
|
|
|
<p>那么什么类型是可 <code>Copy</code> 的呢?可以查看给定类型的文档来确认,不过作为一个通用的规则: <strong>任何基本类型的组合可以 <code>Copy</code> ,不需要分配内存或某种形式资源的类型是可以 <code>Copy</code> 的</strong>。如下是一些 <code>Copy</code> 的类型:</p>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>所有整数类型,比如 <code>u32</code></li>
|
|
|
|
|
<li>布尔类型,<code>bool</code>,它的值是 <code>true</code> 和 <code>false</code></li>
|
|
|
|
|
<li>所有浮点数类型,比如 <code>f64</code></li>
|
|
|
|
|
<li>字符类型,<code>char</code></li>
|
|
|
|
|
<li>元组,当且仅当其包含的类型也都是 <code>Copy</code> 的时候。比如,<code>(i32, i32)</code> 是 <code>Copy</code> 的,但 <code>(i32, String)</code> 就不是</li>
|
|
|
|
|
<li>不可变引用 <code>&T</code> ,例如<a href="#%E8%BD%AC%E7%A7%BB%E6%89%80%E6%9C%89%E6%9D%83">转移所有权</a>中的最后一个例子,<strong>但是注意: 可变引用 <code>&mut T</code> 是不可以 Copy的</strong></li>
|
|
|
|
|
</ul>
|
|
|
|
|
<h2 id="函数传值与返回"><a class="header" href="#函数传值与返回">函数传值与返回</a></h2>
|
|
|
|
|
<p>将值传递给函数,一样会发生 <code>移动</code> 或者 <code>复制</code>,就跟 <code>let</code> 语句一样,下面的代码展示了所有权、作用域的规则:</p>
|
|
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
|
|
|
|
|
let s = String::from("hello"); // s 进入作用域
|
|
|
|
|
|
|
|
|
|
takes_ownership(s); // s 的值移动到函数里 ...
|
|
|
|
|
// ... 所以到这里不再有效
|
|
|
|
|
|
|
|
|
|
let x = 5; // x 进入作用域
|
|
|
|
|
|
|
|
|
|
makes_copy(x); // x 应该移动函数里,
|
|
|
|
|
// 但 i32 是 Copy 的,所以在后面可继续使用 x
|
|
|
|
|
|
|
|
|
|
} // 这里, x 先移出了作用域,然后是 s。但因为 s 的值已被移走,
|
|
|
|
|
// 所以不会有特殊操作
|
|
|
|
|
|
|
|
|
|
fn takes_ownership(some_string: String) { // some_string 进入作用域
|
|
|
|
|
println!("{}", some_string);
|
|
|
|
|
} // 这里,some_string 移出作用域并调用 `drop` 方法。占用的内存被释放
|
|
|
|
|
|
|
|
|
|
fn makes_copy(some_integer: i32) { // some_integer 进入作用域
|
|
|
|
|
println!("{}", some_integer);
|
|
|
|
|
} // 这里,some_integer 移出作用域。不会有特殊操作
|
|
|
|
|
</code></pre></pre>
|
|
|
|
|
<p>你可以尝试在 <code>takes_ownership</code> 之后,再使用 <code>s</code>,看看如何报错?例如添加一行 <code>println!("在move进函数后继续使用s: {}",s);</code>。</p>
|
|
|
|
|
<p>同样的,函数返回值也有所有权,例如:</p>
|
|
|
|
|
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
|
|
|
|
|
let s1 = gives_ownership(); // gives_ownership 将返回值
|
|
|
|
|
// 移给 s1
|
|
|
|
|
|
|
|
|
|
let s2 = String::from("hello"); // s2 进入作用域
|
|
|
|
|
|
|
|
|
|
let s3 = takes_and_gives_back(s2); // s2 被移动到
|
|
|
|
|
// takes_and_gives_back 中,
|
|
|
|
|
// 它也将返回值移给 s3
|
|
|
|
|
} // 这里, s3 移出作用域并被丢弃。s2 也移出作用域,但已被移走,
|
|
|
|
|
// 所以什么也不会发生。s1 移出作用域并被丢弃
|
|
|
|
|
|
|
|
|
|
fn gives_ownership() -> String { // gives_ownership 将返回值移动给
|
|
|
|
|
// 调用它的函数
|
|
|
|
|
|
|
|
|
|
let some_string = String::from("hello"); // some_string 进入作用域.
|
|
|
|
|
|
|
|
|
|
some_string // 返回 some_string 并移出给调用的函数
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// takes_and_gives_back 将传入字符串并返回该值
|
|
|
|
|
fn takes_and_gives_back(a_string: String) -> String { // a_string 进入作用域
|
|
|
|
|
|
|
|
|
|
a_string // 返回 a_string 并移出给调用的函数
|
|
|
|
|
}
|
|
|
|
|
</code></pre></pre>
|
|
|
|
|
<p>所有权很强大,避免了内存的不安全性,但是也带来了一个新麻烦: <strong>总是把一个值传来传去来使用它</strong>。 传入一个函数,很可能还要从该函数传出去,结果就是语言表达变得非常啰嗦,幸运的是,Rust 提供了新功能解决这个问题。</p>
|
|
|
|
|
<h2 id="课后练习"><a class="header" href="#课后练习">课后练习</a></h2>
|
|
|
|
|
<blockquote>
|
|
|
|
|
<p><a href="https://zh.practice.rs/ownership/ownership.html">Rust By Practice</a>,支持代码在线编辑和运行,并提供详细的<a href="https://github.com/sunface/rust-by-practice/blob/master/solutions/ownership/ownership.md">习题解答</a>。</p>
|
|
|
|
|
</blockquote>
|
|
|
|
|
|
|
|
|
|
<div id="giscus-container"></div>
|
|
|
|
|
</main>
|
|
|
|
|
|
|
|
|
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
|
|
|
<!-- Mobile navigation buttons -->
|
|
|
|
|
<a rel="prev" href="../../basic/ownership/index.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" href="../../basic/ownership/borrowing.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/ownership/index.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" href="../../basic/ownership/borrowing.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 type="text/javascript">
|
|
|
|
|
window.playground_copyable = true;
|
|
|
|
|
</script>
|
|
|
|
|
<script src="../../ace.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../editor.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../mode-rust.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../theme-dawn.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../theme-tomorrow_night.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script src="../../book.js" type="text/javascript" charset="utf-8"></script>
|
|
|
|
|
<script type="text/javascript" charset="utf-8">
|
|
|
|
|
var pagePath = "basic/ownership/ownership.md"
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Custom JS scripts -->
|
|
|
|
|
<script type="text/javascript" src="../../assets/custom.js"></script>
|
|
|
|
|
<script type="text/javascript" src="../../assets/bigPicture.js"></script>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|