kandi background

rust | Empowering everyone to build reliable and efficient software | Compiler library

Download this library from

kandi X-RAY | rust Summary

rust is a Rust library typically used in Utilities, Compiler applications. rust has no bugs, it has no vulnerabilities and it has medium support. However rust has a Non-SPDX License. You can download it from GitHub, GitLab.
This is the main source code repository for Rust. It contains the compiler, standard library, and documentation.

kandi-support Support

  • rust has a medium active ecosystem.
  • It has 65482 star(s) with 9062 fork(s). There are 1477 watchers for this library.
  • There were 5 major release(s) in the last 6 months.
  • There are 7959 open issues and 35235 have been closed. On average issues are closed in 169 days. There are 531 open pull requests and 0 closed requests.
  • It has a neutral sentiment in the developer community.
  • The latest version of rust is 1.60.0

quality kandi Quality

  • rust has 0 bugs and 0 code smells.

securitySecurity

  • rust has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
  • rust code analysis shows 0 unresolved vulnerabilities.
  • There are 0 security hotspots that need review.

license License

  • rust has a Non-SPDX License.
  • Non-SPDX licenses can be open source with a non SPDX compliant license, or non open source licenses, and you need to review them closely before use.

buildReuse

  • rust releases are available to install and integrate.
  • Installation instructions, examples and code snippets are available.
  • It has 9568 lines of code, 325 functions and 135 files.
  • It has high code complexity. Code complexity directly impacts maintainability of the code.
Top functions reviewed by kandi - BETA

Coming Soon for all Libraries!

Currently covering the most popular Java, JavaScript and Python libraries. See a SAMPLE HERE.
kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.

rust Key Features

Empowering everyone to build reliable and efficient software.

rust Examples and Code Snippets

  • Installing from Source
  • Building on a Unix-like system
  • Building on Windows
  • Configure and Make
  • Building Documentation
  • Why is Rust NLL not working for multiple borrows in the same statement?
  • Next failed to load SWC binary
  • Emulate BTreeMap::pop_last in stable Rust
  • Unable to specify `edition2021` in order to use unstable packages in Rust
  • What is the idiomatic way to do something when an Option is either None, or the inner value meets some condition?
  • Why does iteration over an inclusive range generate longer assembly in Rust?
  • How can I get a slice from an Option in Rust?
  • Why is `PartialOrd` not blanket-implemented for all types that implement `Ord`?
  • What is "<[_]>" in Rust?
  • How to initialize a vector with values 0 to n?

Installing from Source

./x.py <subcommand> [flags]

Community Discussions

Trending Discussions on rust
  • Why is Rust NLL not working for multiple borrows in the same statement?
  • Next failed to load SWC binary
  • Emulate BTreeMap::pop_last in stable Rust
  • Match ergonomics and & pattern
  • What is the built-in `#[main]` attribute?
  • Difference between `cargo doc` and `cargo rustdoc`
  • Unable to specify `edition2021` in order to use unstable packages in Rust
  • What is the idiomatic way to do something when an Option is either None, or the inner value meets some condition?
  • What is the official Rust guidance for interoperability with C++, in particular passing and returning structs as arguments?
  • Why does iteration over an inclusive range generate longer assembly in Rust?
Trending Discussions on rust

QUESTION

Why is Rust NLL not working for multiple borrows in the same statement?

Asked 2022-Apr-12 at 00:43

First, I tried something like this:

let mut vec = vec![0];
vec.rotate_right(vec.len());

It can't be compiled because:

error[E0502]: cannot borrow `vec` as immutable because it is also borrowed as mutable

I thought that the Rust borrow checker could be smarter than this, so I found something called NLL, and it should solve this problem.

I tried the sample:

let mut vec = vec![0];
vec.resize(vec.len(), 0);

It could work, but why is it not working with rotate_right? Both of them take a &mut self. What's going on?

ANSWER

Answered 2022-Apr-12 at 00:43

It is definitely an interesting one.

They are similar - but not quite the same. resize() is a member of Vec. rotate_right(), on the other hand, is a method of slices.

Vec<T> derefs to [T], so most of the time this does not matter. But actually, while this call:

vec.resize(vec.len(), 0);

Desugars to something like:

<Vec<i32>>::resize(&mut vec, <Vec<i32>>::len(&vec), 0);

This call:

vec.rotate_right(vec.len());

Is more like:

<[i32]>::rotate_right(
    <Vec<i32> as DerefMut>::deref_mut(&mut vec),
    <Vec<i32>>::len(&vec),
);

But in what order?

This is the MIR for rotate_right() (simplified a lot):

fn foo() -> () {
    _4 = <Vec<i32> as DerefMut>::deref_mut(move _5);
    _6 = Vec::<i32>::len(move _7);
    _2 = core::slice::<impl [i32]>::rotate_right(move _3, move _6);
}

And this is the MIR for resize() (again, simplified a lot):

fn foo() -> () {
    _4 = Vec::<i32>::len(move _5);
    _2 = Vec::<i32>::resize(move _3, move _4, const 0_i32);
}

In the resize() example, we first call Vec::len() with a reference to vec. This returns usize. Then we call Vec::resize(), when we have no outstanding references to vec, so mutably borrowing it is fine!

However, with rotate_right(), first we call <Vec<i32> as DerefMut>::deref_mut(&mut vec). This returns &mut [i32], with its lifetime tied to vec. That is, as long as this reference (mutable reference!) is alive, we are not allowed to use have any other reference to vec. But then we try to borrow vec in order to pass the (shared, but it doesn't matter) reference to Vec::len(), while we still need to use the mutable reference from deref_mut() later, in the call to <[i32]>::rotate_right()! This is an error.

This is because Rust defines an evaluation order for operands:

Expressions taking multiple operands are evaluated left to right as written in the source code.

Because vec.resize() is actually (&mut *vec).rotate_right(), we first evaluate the dereference+reference, then the arguments:

let dereferenced_vec = &mut *vec;
let len = vec.len();
dereferencec_vec.rotate_right(len);

Which is obviously a violation of the borrow rules.

On the other hand, vec.resize(vec.len()) has no work to do on the callee (vec), and so we first evaluate vec.len(), and then the call itself.

Solving this is as easy as extracting the vec.len() to a new line (new statement, to be precise), and the compiler also suggests that.

Source https://stackoverflow.com/questions/71823410

Community Discussions, Code Snippets contain sources that include Stack Exchange Network

Vulnerabilities

No vulnerabilities reported

Install rust

Read "Installation" from The Book.

Support

If you’d like to build the documentation, it’s almost the same:. The generated documentation will appear under doc in the build directory for the ABI used. I.e., if the ABI was x86_64-pc-windows-msvc, the directory will be build\x86_64-pc-windows-msvc\doc.