小编Ana*_*kov的帖子

为什么从字符串长度开始的切片不会引起恐慌?

fn main() {
    let s = String::from("hello");
    let hello = &s[5..];
    println!("{}", hello);
}
Run Code Online (Sandbox Code Playgroud)

为什么这段代码没有恐慌?我认为这是索引超出内容大小的条件.此代码示例的输出不显示任何内容.

syntax rust

6
推荐指数
1
解决办法
181
查看次数

如何访问枚举变量未命名字段?

我想打印没有命名字段的元组枚举数据。

元组是将一些具有多种类型的其他值分组为一个复合类型的通用方法。

#[derive(Debug)]
enum Coin {
    Penny(String),
    Nickel { id: String },
}

fn main() {
    let penny = Coin::Penny(String::from("penny"));
    let nickel: Coin = Coin::Nickel { id: String::from("nickel") };

    println!("{} {:?} ", penny.0, penny);
    println!("{:?}", nickel);
}
Run Code Online (Sandbox Code Playgroud)

在此示例中,Nickel是一个类似结构体的枚举变体,而 则Penny简称为枚举变体

我收到编译器错误:

#[derive(Debug)]
enum Coin {
    Penny(String),
    Nickel { id: String },
}

fn main() {
    let penny = Coin::Penny(String::from("penny"));
    let nickel: Coin = Coin::Nickel { id: String::from("nickel") };

    println!("{} {:?} ", penny.0, penny);
    println!("{:?}", nickel); …
Run Code Online (Sandbox Code Playgroud)

enums rust

6
推荐指数
2
解决办法
4104
查看次数

为什么Rust编译器在使用Rust 1.31时会破坏借用规则?

我正在研究Rust by Example并从"Alias"页面运行代码:

struct Point {
    x: i32,
    y: i32,
    z: i32,
}

fn main() {
    let mut point = Point { x: 0, y: 0, z: 0 };

    {
        let borrowed_point = &point;
        let another_borrow = &point;

        // Data can be accessed via the references and the original owner
        println!(
            "Point has coordinates: ({}, {}, {})",
            borrowed_point.x, another_borrow.y, point.z
        );

        // Error! Can't borrow point as mutable because it's currently
        // borrowed as immutable.
        let …
Run Code Online (Sandbox Code Playgroud)

rust borrow-checker borrowing

5
推荐指数
2
解决办法
225
查看次数

找不到任务“ docs”。你是说“做”吗?

如何为混合项目生成文档?怎么做呢?

使用Elixir混合项目的过程:

  • 我通过mix new greeter命令生成一个项目。
  • 我在greeter.ex文件中添加了一块注释。
  • 我将依赖项添加到mix.exs文件中。
  • 我尝试通过mix docs 命令生成文档。

mix help 无法docs在可能的选项列表中提供任务:

mix                   # Runs the default task (current: "mix run")
mix app.start         # Starts all registered apps
mix app.tree          # Prints the application tree
mix archive           # Lists installed archives
mix archive.build     # Archives this project into a .ez file
mix archive.install   # Installs an archive locally
mix archive.uninstall # Uninstalls archives
mix clean             # Deletes generated application files …
Run Code Online (Sandbox Code Playgroud)

documentation elixir-mix elixir

2
推荐指数
1
解决办法
130
查看次数