简体   繁体   English

如何使用电源shell安装Rust?

[英]How to install Rust using power shell?

I want to install rust automatically via command line on Windows.我想通过 Windows 上的命令行自动安装 rust。 It doesn't matter if it's power shell or not but I think power shell is the easiest without having to install other stuff manually.电源 shell 与否无关紧要,但我认为电源 shell 是最简单的,无需手动安装其他东西。

I found https://www.powershellgallery.com/packages/AppVeyorBYOC/1.0.170/Content/scripts%5CWindows%5Cinstall_rust.ps1 but at我找到了 https://www.powershellgallery.com/packages/AppVeyorBYOC/1.0.170/Content/scripts%5CWindows%5Cinstall_rust.ps1但在

 Add-SessionPath "$env:USERPROFILE\.cargo\bin"

it says that Add-SessionPath is a cmdlet that does not exist.它说Add-SessionPath是一个不存在的 cmdlet。

I had the same error.我有同样的错误。 I changed that line to this and the script worked with Powershell 7 .我将该行更改为该行,该脚本与Powershell 7一起使用。

$env:Path = "$env:USERPROFILE\.cargo\bin"
# Make new environment variables available in the current PowerShell session:
function reload {
   foreach($level in "Machine","User") {
      [Environment]::GetEnvironmentVariables($level).GetEnumerator() | % {
         # For Path variables, append the new values, if they're not already in there
         if($_.Name -match 'Path$') { 
            $_.Value = ($((Get-Content "Env:$($_.Name)") + ";$($_.Value)") -split ';' | Select -unique) -join ';'
         }
         $_
      } | Set-Content -Path { "Env:$($_.Name)" }
   }
}
Write-Host "Installing Rust..." -ForegroundColor Cyan
$exePath = "$env:TEMP\rustup-init.exe"

Write-Host "Downloading..."
(New-Object Net.WebClient).DownloadFile('https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe', $exePath)

Write-Host "Installing..."
cmd /c start /wait $exePath -y
Remove-Item $exePath

$addPath = "$env:USERPROFILE\.cargo\bin"
[Environment]::SetEnvironmentVariable
     ($addPath, $env:Path, [System.EnvironmentVariableTarget]::Machine)

reload

cargo --version
rustup --version
rustc --version

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM