我总是搞砸了怎么用const int*,const int * const和int const *正确的.是否有一套规则来定义您能做什么和不能做什么?
我想知道在任务,传递到职能等方面所做的所有事情和所有不应做的事情.
我想知道它们之间的区别
const int* ptr;
Run Code Online (Sandbox Code Playgroud)
和
int * const ptr;
Run Code Online (Sandbox Code Playgroud)
以及它是如何工作的.
我很难理解或记住这一点.请帮忙.
我想将一个数组传递给一个函数并更改其中的内容.我怎么能这样做,这是我下面的代码,但当然它不起作用.
fn change_value(mut arr: &[i32]) {
arr[1] = 10;
}
fn main() {
let mut arr: [int; 4] = [1, 2, 3, 4];
change_value(&arr);
println!("this is {}", arr[1]);
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:"无法分配给不可变的vec内容__CODE__".
我一直在寻找,但作为一个漂亮的新手Rust程序员,我找不到任何东西.另外,Rust对其语言进行了大量修改并没有帮助,因此很多方法都被弃用或删除了.
我正在尝试将可变向量转换为Rust中的不可变向量.我认为这会起作用,但它没有:
let data = &mut vec![];
let x = data; // I thought x would now be an immutable reference
Run Code Online (Sandbox Code Playgroud)
如何将可变引用转换为不可变绑定?
如果我有一个绑定到结构的不可变变量,Rust通常不允许我改变结构的字段或拥有的子结构的字段.
但是,如果该字段是一个可变引用,Rust 将允许我改变引用的对象,尽管我的绑定是不可变的.
为什么允许这样做?它与Rust的不变性的正常规则不一致吗?
Rust 不会让我通过不可变引用做同样的事情,因此不可变引用具有与不可变引用不同的行为.
代码示例:
struct Bar {
val: i32,
}
struct Foo<'a> {
val: i32,
bar: Bar,
val_ref: &'a mut i32,
}
fn main() {
let mut x = 5;
{
let foo = Foo {
val: 6,
bar: Bar { val: 15 },
val_ref: &mut x
};
// This is illegal because binding is immutable
// foo.val = 7;
// Also illegal to mutate child structures
// foo.bar.val = 20;
// This is fine …Run Code Online (Sandbox Code Playgroud) 采取方法self和采取方法&self甚至是方法有什么区别&mut self?
例如
impl SomeStruct {
fn example1(self) { }
fn example2(&self) { }
fn example3(&mut self) { }
}
Run Code Online (Sandbox Code Playgroud)
假设我想实现一种将结构体漂亮地打印到标准输出的方法,我应该采用&self吗?我猜self也有效?我不确定何时使用什么。
我正在阅读Rust Book的第二版,我在迭代器部分找到了以下示例:
let v1 = vec![1, 2, 3];
let v1_iter = v1.iter();
for val in v1_iter {
println!("Got: {}", val);
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器不抱怨v1_iter是不可变的?这本书说for循环取得了所有权,v1_iter并使其在幕后变得可变,但是你可以将一个不可变变量转换为可变变量吗?
作为学习Rust的练习,我决定实现一个Bit Vector库,其灵感来自std::vec::Vec于提供哪些方法.
我有以下代码:
extern crate num;
use std::cmp::Eq;
use std::ops::{BitAnd,BitOrAssign,Index,Shl};
use num::{One,Zero,Unsigned,NumCast};
pub trait BitStorage: Sized +
BitAnd<Self, Output = Self> +
BitOrAssign<Self> +
Shl<Self, Output = Self> +
Eq + Zero + One + Unsigned + NumCast + Copy {}
impl<S> BitStorage for S where S: Sized +
BitAnd<S, Output = S> +
BitOrAssign<S> +
Shl<S, Output = S> +
Eq + Zero + One + Unsigned + NumCast + Copy {}
pub struct BitVector<S: BitStorage> { …Run Code Online (Sandbox Code Playgroud) 我正在从书中学习 Rust,并且正在处理第 8 章末尾的练习,但是在将单词转换为 Pig Latin 的问题上我遇到了困难。我想具体看看我是否可以将 a 传递&mut String给一个接受 a (也接受切片)的函数&mut str并修改其中引用的字符串,以便将更改反射回外部而不需要 a return,就像在 C 中使用 a 一样char **。
我不太确定我只是搞乱了语法,还是由于 Rust 的严格规则而导致它比听起来更复杂,而我还没有完全掌握这些规则。对于里面的终身错误,to_pig_latin()我记得读过一些解释如何正确处理这种情况的东西,但现在我找不到它,所以如果你也能为我指出它,我将非常感激。
另外,您对我处理字符串内的字符和索引的方式有何看法?
use std::io::{self, Write};
fn main() {
let v = vec![
String::from("kaka"),
String::from("Apple"),
String::from("everett"),
String::from("Robin"),
];
for s in &v {
// cannot borrow `s` as mutable, as it is not declared as mutable
// cannot borrow data in a `&` reference as mutable
to_pig_latin(&mut s);
}
for …Run Code Online (Sandbox Code Playgroud) 此代码在 C++ 中编译:
int x = 5;
int *const px = &x;
int *const *const ppx = &px;
int const *const *const cppx = ppx;
Run Code Online (Sandbox Code Playgroud)
所以我尝试在 Rust 中做同样的事情:
let mut x: i32 = 5;
let px: &mut i32 = &mut x;
let ppx: &&mut i32 = &px;
let cppx: &&i32 = ppx;
Run Code Online (Sandbox Code Playgroud)
但是,这无法编译:
error[E0308]: mismatched types
--> src/main.rs:5:23
|
5 | let cppx: &&i32 = ppx;
| ----- ^^^ types differ in mutability
| |
| expected due to this …Run Code Online (Sandbox Code Playgroud)